Cell contents assignment to a non-cell array object

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
any help will be appriciated

1 Comment

Note: other than confusion between cells and structures that others have answered to, there is a very suspicious line in your code:
filt_sig = eval(something, somethingelse)
Using eval is extremely discouraged. Furthermore, eval only takes one argument and will simply ignore the second.

Sign in to comment.

Answers (2)

You declared main as a struct. Structs do not use braces {}. To fix, convert braces to parentheses (). Or else declare main as a cell array with the cell function rather than the struct function.

1 Comment

I cannot convert braces to paranthesis, It says
Cannot call or index into temporay array
Can you please elaborate on declaring main as a cell array with cell functions

Sign in to comment.

main is declared as a struct so yes, you would get that error if you are trying to assign to it as though it is a cell array which is what
main{i}
will do. I assume what you want is an array of structs which does not need to be a cell array if the structs all have the same fields.

4 Comments

What's the work around for this?
For a statement like this:
main{i}(k).seg_err(a)
to make sense you first need to declare a cell array as e.g
main = cell.empty( 1, length(this.segments) );
Then you would need to initialise each element with a struct that actually contains pre-sized arrays rather than empty elements.
e.g.
myStruct.seg_err = zeros( 1, length( fc ) );
myStruct.sig_err = zeros( 1, length( fc ) );
etc
then inside your outer i loop you would need to create the struct array and assign to the cell array as e.g.
main{i} = repmat( myStruct, [1, length(this.segments{i})] );
then if I haven't missed anything you will have the arrays within your struct ready for the current index of the cell array when you try to do an assignment like the one at the top of this reply.
This is all mostly written from my head though, not fully tested so you may need to iron out some errors.
The basic idea is that you need to have created all your components first before you just try to assign to them.
Which is why it would be much much simpler to use a non-scalar structure instead, which would avoid all of that confusion:
main(i,k).seg_err(a) = ...
Ah! non scalar structure seems to be easier solution, can you please tell me how do i start off with this, like declaration?

Sign in to comment.

Categories

Asked:

JA
on 27 Jul 2016

Commented:

on 27 Jul 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!