Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Need Help using for and running a loop

1 view (last 30 days)
Matt
Matt on 9 Oct 2012
Closed: MATLAB Answer Bot on 20 Aug 2021
Basically i have the following code;
for k = 1:357;
j = 357:1;
hold on
%%3D Elipsoid
%
x0 = jhkmag{k}(:,1)-jhkmag{j}(:,1);
hold on
y0 = dffmag{k}(:,1) - dffmag{j}(:,1);
hold on
z0 = dffmag{k}(:,2) - dffmag{j}(:,2);
rccm{k} = sqrt((0.5)*((x0{k}.^2)+(y0{k}.^2) + (z0{k}.^2)));
hold on
end
end
basically this code should calculate an ellipsoidal and calculate the distance to each point. But I cant get the loop to work it keeps saying that there is a bad cell reference, I really cant get this figured out. The next thing I need it to do is count to the nearest 15 data points, then compare that rccm on a different set of data results. I have no idea how to do this either pleeeeeaaase help

Answers (2)

the cyclist
the cyclist on 9 Oct 2012
If you type
>> dbstop if error
before executing your code, then the code execution will halt when MATLAB hits the line causing the error, and you will be in debug mode. Then, you can go into the editor and see exactly what's happening, in terms of loop variable value, etc., to get more insight into the error.
After you track it down, you can type
>> dbclear if error
to disable the automatic stopping on error.
  2 Comments
Matt
Matt on 9 Oct 2012
it says that its in the line starting with x0 this is what it says;
Bad cell reference operation.
Error in LoopGraphs (line 10) x0 = jhkmag{k}(:,1)-jhkmag{j}(:,1);
10 x0 = jhkmag{k}(:,1)-jhkmag{j}(:,1);
i still don't really understand why it wont do it though
per isakson
per isakson on 9 Oct 2012
Edited: per isakson on 9 Oct 2012
What does
K>> jhkmag{j}
and
K>> jhkmag{k}
show?

Walter Roberson
Walter Roberson on 9 Oct 2012
j = 357:1; defines j as empty. If you want j to count down, use a negative increment,
j = 357:-1:1;
Also, the spacing you used,
for k = 1:357;
j = 357:1;
tends to give the impression that you are expecting the "for" to iterate both variables. A "for" loop only iterates a single variable. If you intend that j be assigned a vector in that statement, I recommend not indenting it to be aligned right under the "k =" of the line above.

This question is closed.

Community Treasure Hunt

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

Start Hunting!