Error: "Cell contents assignment to a non-cell array object". Previously "In an assignment A(I) = B, the number of elements in B and I must be the same."
Show older comments
I think this may be a recurring question here, but nothing seems to help.
- I am having trouble assigning values to the matrix u, which in this case calculates a value each iteration for u; so I made a cell array, but now I see the following problem: Cell contents assignment to a non-cell array object, and jumps into the second part of the if statement, with the same error. I am not achieving that for each iteration of i, the value obtained is stored into the matrix u in the (1,i) position.
- Then I want to plot the matrix u against time (which is defined in the beggining).
- I preallocated zeros in u in the beginning for it to run faster.
Thanks in advance for any help
My code:
clear
clc
close all
% Symbolic
syms wn w t po k u m tf
po=10000;
m=253.3;
k=10000;
wn=sqrt(k/m);
w=pi/06;
C=po/(k-(m*w^2));
u=zeros(1,10);
v=zeros(1,10);
t=[0:0.1:1];
% Step
for i = 2:10
if t<0.6
u{1,i}=[C*sin(w*t)];
%u(i)=round(u(i));
v{i}=[diff(u(i),t)];
else
u{i}= [u(1,7)*cos(wn*(t-0.6))+(v(1,7)/wn)*sin(wn*(t-0.6))];
end
end
plot(t,u)
Answers (1)
Walter Roberson
on 6 Apr 2016
u = cell(1,10);
2 Comments
Pedro López-Velarde Peña
on 6 Apr 2016
Walter Roberson
on 6 Apr 2016
Your underlying problem is this:
t=[0:0.1:1];
defines t as a vector. Then inside your "for i" loop, you have
if t<0.6
that is testing the entire vector, generating a logical vector of results of t<0.6. Then because "if" is defined to consider the condition to be true only when all values in a vector or array are considered true, and there are some values of t that do not match the condition, the "if" is considered false. Your code then proceeds to
u{i}= [u(1,7)*cos(wn*(t-0.6))+(v(1,7)/wn)*sin(wn*(t-0.6))];
Because t is a vector, (t-0.6) is a vector so cos(wn*(t-0.6)) and likewise sin(wn*(t-0.6)) is a vector, so your overall computation is returning a vector. And then you tried to store that entire vector into a single location.
What you should have done is used vectors (not cell arrays) for u, but each time inside the "for i" that you reference t, you need to index t at i. However, v needs to be a cell array
u = zeros(1,10);
v = repmat({0}, 1, 10};
for i = 2:10
if t(i) < 0.6
u(i) = C*sin(w*t(i));
%v{i} = diff(u(i),t(i));
if t == 0 || mod(t(i), 1) ~= 0
error('cannot calculate numeric difference at every %g samples', t(i));
else
v{i} = diff(u(i),t(i));
end
else
u(i) = u(1,7)*cos(wn*(t(i)-0.6)) + (v{1,7}/wn)*sin(wn*(t(i)-0.6));
end
end
There are two diff() routines, one symbolic and one numeric. The symbolic diff() only gets called if one of the inputs is symbolic, but u(i) and t(i) are both numeric, so you are calling the numeric diff() routine. The numeric diff() routine needs the second parameter to be a non-negative integer, so as an optimization I generate an error message directly instead of calling diff() to create an error message that might be harder to trace. v needs to be a cell array because when you finally do get to t == 1, diff(u(i), 1) is the empty array [] .
By the way, you might want to pay attention to the fact that the first i for which t(i) < 0.6 is false, is i = 7, when t(i) = 0.6, so when i = 7, you are doing
u(7) = u(1,7)*cos(wn*(0.6-0.6)) + (v{1,7}/wn)*sin(wn*(0.6-0.6));
with 0.6-0.6 being 0, that will involve cos(0) and sin(0), which will be 1 and 0 respectively. So this will effectively become
u(7) = u(1,7) * 1 + 0
or
u(7) = u(7)
Fortunately for you, you initialized all of u to be 0, so this calculation will be properly defined, the equivalent of
u(7) = 0
Then in the iterations after that, when you refer to u(1,7) it will be 0, making it a useless term, so you could optimize to
u(i) = (v{1,7}/wn) * sin(wn*(t(i)-0.6));
The value of v{1,7} is, in turn, also 0, because you initialized v to all 0 and you did not assign anything else to to v{1,7} because you stopped assigning to v once t<0.6 is false. So you can further optimize this expression right down to
u(i) = 0;
and then since you initialized u to all 0, you can skip that assignment too.
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!