0

I am getting the above mentioned error while going through multiple loops. I really don't know how to explain the problem, But i will try my best

code:

function this = tempaddfilt(this,varargin)
            fc = linspace(1,200,(200/0.5));
            main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{});
            for a = 1:length(fc) % fc
                q = 0;
                w = 0
                for i = 1:length(this.segments) % total number signal
                    for k = 1:length(this.segments{i}) % total number of segments
                         filt_sig = eval(this.segments{i}(k).signal,this.segments{i}(k).signal(1)); % apply filter to the ith singal and kth segemnt
                        filt_sig = filt_sig';
                        main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref); % calculate the standard divitation of the filtered signal and previously calculated signal.
                        q = q+main{i}(k).seg_err(a); add all the error of the segments for the same FC
                        
                    end
                    
                    main{i}(1).sig_err(a) = q; % assign the sum of all error of the all segemnts of the same signal
                    w = w+main{i}(1).sig_err(a); % add all the error of the signals
                end
                main.filt_err = w; % assign the sum of all error of the all signals
            end
            this.error_norm = [this.error_norm ;main];
        end
        
    end

Basically I have 3 loops, 1st loop is for fc, 2nd loop is for signal and 3rd loop is for segemnts of the singal. Program works fine when fc = 1.

But when fc is 2, i get the following error:

Cell contents assignment to a non-cell array object.

in the line:

main{i}(k).seg_err(a) = std(filt_sig-this.segments{i}(k).ref);

that is when i =1 , k=1 ,a = 2

user5603723
  • 195
  • 2
  • 11
  • the error is saying you are trying to assign the contents of cells into an Array, like A = Cell(1,:) ; you can try `std(cell2double(filt_sig-this.segments{i}(k).ref))` and see what happens. – GameOfThrows Jul 27 '16 at 10:54
  • @GameOfThrows, i did not find anything called cell2double. – user5603723 Jul 27 '16 at 11:21
  • @CaptainFuture, i did debug, i stopped the program on that line and caluclted the value on the command window. on both instance it returns a value. – user5603723 Jul 27 '16 at 11:43
  • instead of implementing it in the code, i again stopped the program and checked 'iscell(std(filt_sig-this.segments{i}(k).ref))' in the command window @ fc = 1, fc = 2, both returned '0', i really think problem is on the Left hand side. – user5603723 Jul 27 '16 at 11:58
  • @user5603723 yep, sorry, I meant str2double - long day at office. – GameOfThrows Jul 27 '16 at 12:23
  • @GameOfThrows, thanks. But did not work. I feel the error is on left hand side. – user5603723 Jul 27 '16 at 12:28
  • what does this `std(filt_sig-this.segments{i}(k).ref)` give, and what does `main{i}(k).seg_err(a)` give? – GameOfThrows Jul 27 '16 at 12:29
  • 'std(filt_sig-this.segments{i}(k).ref)' gives me a value, say '2.345'. and i want 'main{i}(k),seg_err' to be an array – user5603723 Jul 27 '16 at 12:35
  • I see, and seg_err field only have numerics? What happens if you change it to main{i}(k),seg_err{a} instead of (a)? – GameOfThrows Jul 27 '16 at 12:36
  • same error, 'Cell contents assignment to a non-cell array object.' – user5603723 Jul 27 '16 at 12:40
  • I asked the same question in mathworks, they're trying to help, but i don't seem to understand, if you can please take a look: http://de.mathworks.com/matlabcentral/answers/297357-cell-contents-assignment-to-a-non-cell-array-object – user5603723 Jul 27 '16 at 12:47
  • sorry for being dumb, long day. What's solution? i tried changing it to 'main = struct('seg_err',[],'sig_err',[],'filt_err',[],'fc',[])' doesn't work either – user5603723 Jul 27 '16 at 13:03
  • @CaptainFuture, any suggestions? – user5603723 Jul 27 '16 at 13:20

1 Answers1

1

The problem seems to be with the way how you want to access the members of the main struct dynamically. You declared main as a struct, with

main = struct('seg_err',{},'sig_err',{},'filt_err',{},'fc',{});

BUT struct members can not be accessed by using braces {}. Here is a reference to a previous discussion related to dynamic indexing of struct arrays. So, basically, the problem is with "main{i}", which is not a valid way of dynamically indexing members of a struct.

Try following. Change the struct declaration to explanation

main = struct('seg_err',[],'sig_err',[],'filt_err',[],'fc',[]);

Then, extract the field names by

FieldNames = fieldnames(main);

Then, you can reference the struct members like in

for 
loopIndex = 1:numel(FieldNames) 
main.(FieldNames{loopIndex})(1).seg_err(1) = 1; 
end 
Community
  • 1
  • 1
Captain Future
  • 329
  • 1
  • 5