How can I use a for loop to reassign matrix names?
Show older comments
Hi there! I am trying to write a 'for' loop which does the following:
for i = 1:9
a = a0i; b = b0i; cx = c0i(:, 1); cy = c0i(:, 2); t = t00i;
foneplotp(a, b, cx, cy, t, period)
end
I have matrices labeled a01, a02, a03, etc. in my workspace, but when I try to run this, I get the error:
Undefined function or variable 'a0i'.
Is there a way to do this with a 'for' loop in MATLAB?
Thanks!
Miranda
Accepted Answer
More Answers (1)
Image Analyst
on 7 May 2014
Why complicate it? Just do
foneplotp(a01, b01, c01(:,1), c01(:,2), t001, period);
foneplotp(a02, b02, c02(:,1), c02(:,2), t002, period);
foneplotp(a03, b03, c03(:,1), c03(:,2), t003, period);
foneplotp(a04, b04, c04(:,1), c04(:,2), t004, period);
foneplotp(a05, b05, c05(:,1), c05(:,2), t005, period);
foneplotp(a06, b06, c06(:,1), c06(:,2), t006, period);
foneplotp(a07, b07, c07(:,1), c07(:,2), t007, period);
foneplotp(a08, b08, c08(:,1), c08(:,2), t008, period);
foneplotp(a09, b09, c09(:,1), c09(:,2), t009, period);
It's only 9 lines long and a heck of a lot simpler to understand and maintain than the crazy workaround mentioned in the FAQ
3 Comments
Miranda
on 7 May 2014
Image Analyst
on 7 May 2014
Edited: Image Analyst
on 7 May 2014
You should NOT have 100 differently named variables! How did you ever create those in the first place? Did you use the method that the FAQ shows you but advises against ? Well you can do the same thing to get those names and assign them to other variables like a,b,c, etc.
You should use a 2D array instead of different names. This is what the FAQ strongly recommends . Please don't do what you are asking.
Miranda
on 7 May 2014
Categories
Find more on Profile and Improve Performance 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!