In an assignment A(I) = B, the number of elements in B and I must be the same.
Show older comments
Hi all I'm facing some error made me crazy :S
I'm using that code
for fa=10:10:50
V1=(V/sqrt(3)/50)*fa; % voltage changes with frequency
ns1=(60*fa)/p;
ws1=(2*pi*fa)/p;
n1=0:5:ns1;
s1=(ns1-n1)./ns1;
T1=(3*V1.^2./(ws1))*(R2./s1)./((R1+(R2./s1)).^2+((fa/50)*(x1+x2))^2);
plot(n1,T1)
hold on
[ni,Ti] = intersections(n1,T1,ng,Tl);
y(cnta) = Ti;
x(cnta) = ni;
cnta=cnta+1;
end
for fb=60:10:100
V2=V/sqrt(3) ; % in high frequency voltage is const
ns2=(60*fb)/p;
ws2=(2*pi*fb)/p;
n2=0:5:ns2;
s2=(ns2-n2)./ns2;
T2=(3*V2.^2./(ws2))*(R2./s2)./((R1+(R2./s2)).^2+((fb/50)*(x1+x2))^2);
plot(n2,T2,'r')
hold on
[nw,Tw] = intersections(n2,T2,ng,Tl);
z(cntb) = Tw;
c(cntb) = nw;
cntb=cntb+1;
end
and error is
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Untitled (line 39) z(cntb) = Tw;
note that i copied the code from above for loop to the second for loop and the error in the second one :( and all variables are declared before :)
waiting for ur help and thanks in advance :)
Answers (1)
Have a look at this:
A([1 2 3]) = [8 7 6 8 7 9] % Error
A([1 2 3]) = [8 7] % Error
A([1 2 3]) = [8 7 6] % No error
We get the same error with both of the first assignment attempts. So in your code, cntb has 1 element (as far as I can see) and Tw has more or less than 1 element. That is where you need to start your investigation of the code.
z(cntb) = Tw % numel(Tw) ~= numel(cntb)
7 Comments
Matt Fig
on 1 Dec 2012
ahmed comments:
cntb and cnta are counters every time increases by one till for loop ends Tw and nw have one value in one for loop execution
ahmend, if cntb is a scalar, then Tw does not always have one value. Look at the error message: MATLAB didn't just start lying to you or making things up about your code!
Put a line between these two lines of code to see what I mean:
[nw,Tw] = intersections(n2,T2,ng,Tl);
% Put this next line in your code!
if numel(Tw)~=1,error('Tw wrong size'),end
z(cntb) = Tw;
Matt Fig
on 1 Dec 2012
ahmed comments (Again as an 'Answer'!):
i got the error message wrong size :( but i cant get it till now how come that it can be applied in the first loop and in the second loop doesn't work :S although the both loops same !
The loop are not the same, or you would get the same result. You seem to think MATLAB has a mind of its own, but it doesn't. MATLAB is deterministic - it does what you tell it to do.
So you need to examine why the INTERSECTIONS function is returning the second output argument as a non-scalar. I don't have this function so I don't even know what it is supposed to do For all I know, the INTERSECTIONS function is supposed to return Tw with more (or less!) than one element when passed certain kinds of data....
ahmed
on 1 Dec 2012
Matt Fig
on 1 Dec 2012
Did you read the help for that function before you used it? The author provides a help file, which you can read by typing this:
help intersections
I suggest you get in the habit of reading about the functions you are using! According to the help,
% Computes the (x,y) locations where two curves intersect.
So if the curves intersect at more than one location, you will get more than one x,y pair. If the curves intersect in NO locations, x and y will be empty. Now you need to figure out what to do in either case before you try to pass the results to z.
if numel(Tw)==1
z(cntb) = Tw; % For example....
else
% Whatever you want to do here.
end
ahmed
on 1 Dec 2012
Categories
Find more on App Building 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!