MATLAB: How to use a for loop to reassign matrix names

assign variablesfor loopindexingmatrix manipulationnot recommmended code

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

Best Answer

It’s horrible programming style, but it’s possible. So long as your matrices already exist and you simply need to call them, here is an example of what you can do:
a01 = pi;
c01 = [5 7];
for i = 1:9
a = eval(sprintf('a0%d',i))
cx = eval(sprintf('c0%d(:, 1)',i))
end
I used a01 and c01 to test the code, so remove those in your actual code. I listed them here so you can test them to see how it works.