# MATLAB - changing variable name with each iteration?

• MATLAB
Hi all, I am sure this will be easy for one of you clever people but it has got me stumped.

I am working on a physics project and I am using MATLAB to acquire some data.

So basically I have a time varying current signal coming in and I am collecting the current data and plotting it against time. Now I want to do this several times using a for loop. I need to keep the data acquired with each loop and be able to use each set separately.

i.e. current = data_in

My problem is how do stop it from overwriting the current vector next time it loops?

Ideally I would like it to save the vectors in the following way:

current_1 = x x x x x x x x x x x x x x
(loop again)
current_2 = x x x x x x x x x x x x x x
(loop again)
current_3 = x x x x x x x x x x x x x x

and so on...

So is there a way I can change the current variable name each time the loop goes round.

Sorry if this seems a little confusing, I am confused myself.

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
store the info into a vector that is a function of the counter variable.

I know that indexing could be used if current was a single value, like in the following:

i = 1 ;

for x = 0:1:10

current(i) = x^2 ;
i = i+1 ;
end

But if each x value was a vector rather than an integer how could I index the different current vectors.

Last edited:
store the info into a vector that is a function of the counter variable.
Sorry, could you elaborate a little?

The data is a function of the counter variable, but each for each counter value a vector is produced. I need to save and then access each of these vectors rather than it just being overwritten.

Last edited:
I should also have said that in my first post data_in is a vector rather than a single value. data_in is the thing changing with each loop according to the loop counter.

The loop counter adjusts the frequency of a sine wave, the data_in is then 1000 samples of the wave.

chroot
Staff Emeritus
Gold Member
Use a two-dimensional vector, i.e. a matrix.

- Warren

Thanks, got it now

change a variable name

The matrix solution is cool, but if you really want to change the variable name in a loop, so use varname_1, varname_2, and so on, you'd better use eval function (write help eval in MATLAB), so, for example:

for iter=1:10
% varname_i=3*(i^2);
eval(['varname_' num2str(i) '=3*' num2str(i) '^2;']);
end

You can remove the first ; to have echo.

Kind regards

Warren could you explain what was concluded here?
I am in the same position as the guy who asked this question...

rteng,

when iterating through the loop, you can store each vector as a separate row within a matrix.

for example:

for i=1:100;
AcquiredData= %however you acquire here-ish%;
AllData(i,:)=AcquiredData;
end

basically use AcquiredData as a variable that changes each loop, and AllData is a variable that gets a new row with the new data every loop.

Karto, you are the best!!! I spent over two hours trying to find this function in Matlab and couldn't!!! It is not very important to what I am doing, but I like doing things my way, and I would not rest while I didn't find out either how to do it or that it was impossible to do it.
Thank you so much!!!

The matrix solution is cool, but if you really want to change the variable name in a loop, so use varname_1, varname_2, and so on, you'd better use eval function (write help eval in MATLAB), so, for example:

for iter=1:10
% varname_i=3*(i^2);
eval(['varname_' num2str(i) '=3*' num2str(i) '^2;']);
end

You can remove the first ; to have echo.

Kind regards

I just came across this and it was very helpful.

For those that might not get what karto was posting (it took me a little while to get the hang of it):
The eval function requires quotations around regular lines of matlab code because anything else is a string (text, characters, no numerical value). This function merely splices together text manipulation and numerical calculations. An annotated m-file example:

function evalfun
for i=1:10
eval(['A_' num2str(i) '=i'])
end

What this will do is run a for loop 10 times and create:
A_1=1
A_2=2
etc. up to
A_10=10

It converts the number stored as variable i to a text character and adds it to A_. Then the next part in parentheses uses the number stored in variable i for some sort of calculation.

A slight modification:

function evalfun2
for i=1:10
eval(['A_' num2str(i) '=' num2str(i)])
end

This function would create the same visual output but you would not be able to use the values stored in the A_ variables in any calculation because what is stored is a string or in other words a text character.

Following a similar problem that was posted....

I have data , which consists of 3 columns related to 3 parameters H, T and E (Height, period and efficiency) and herewith, I would like to create a scatter diagram.

the name of every cell would carry the name of the H and T parameter and that 'cell' should contain the different efficiencies...

So:
- How managed to create a name for all these cells e.g.: Cell_H_T
for h=1:10
for t=1:10
eval(['Cell_' num2str(h) '_' num2str(t) '=']);
end
end

- how can I adress every matrix individually afterwards?
e.g.: I analyse the data and if appropriate I would like to add that value into cell_H_T...

Last edited:
Hi everyone I am Rihan Haque. I am a M.Sc. student and using Matlab for my physics project but i'm having problem with the .m file that i have written to read images.
in my .m file i have written:

image1 = imread('apple_1' , 'tif'); % 'apple_1' is the image name and 'tif' is the format
m1 = double(image1);
m2 = double(image2);
cor_coef = corr2(m1,m2); % corr2 is a Matlab function to evaluate correlation coefficeient

now the problem is that i have to change the input string 'apple_2' to 'apple_3' manually to get the correlation coefficient of 'apple_1' with 'apple_3'. so u can easily understand that i have to modify my .m file 50 times to get correlation coefficient of 50 images. it is very time consuming and annoying.
so, I kindly request you to help me writing a for loop (or any other way) so that the .m file automatically changes the string and reads images one bye one.

I Thank you all in advance...

Hi.
I've read the previous posts but i still can't do what i want.

What i want to do is this:

for each loop i want to create a new matrix with a different name, and ask for the user to insert some values, for example

n= number of resources for the i task
n(1) is the number of resources for task number 1
n(2) is the number of resources for task number 2 and so on

for ii=1:n(i)​
fprintf('\nInsert the classifications of resource %d (in vector form)\n', ii);​

end​
end

In the first outside loop the matrix created should be called 'r1' and then for the inside loop the user should be asked to insert the values of each row. Let me give you an example:

n=[2 3];

Insert the classifications of resource 1 (in vector form): [x x x x]
Insert the classifications of resource 2 (in vector form): [y y y y]

Insert the classifications of resource 1 (in vector form): [z z z z]
etc etc...

The result that i want is

r1=[x x x x; y y y y]
r2=[z z z z; c c c c; b b b b]

The previous post refers to some functions like eval and i found a function called genvarname in the matlab help but i still don't know how to use them.

I hope I've explained myself well so... can anyone help please?? P.S. Sorry for the long post

Hi everyone I am Rihan Haque. I am a M.Sc. student and using Matlab for my physics project but i'm having problem with the .m file that i have written to read images.
in my .m file i have written:

image1 = imread('apple_1' , 'tif'); % 'apple_1' is the image name and 'tif' is the format
m1 = double(image1);
m2 = double(image2);
cor_coef = corr2(m1,m2); % corr2 is a Matlab function to evaluate correlation coefficeient

now the problem is that i have to change the input string 'apple_2' to 'apple_3' manually to get the correlation coefficient of 'apple_1' with 'apple_3'. so u can easily understand that i have to modify my .m file 50 times to get correlation coefficient of 50 images. it is very time consuming and annoying.
so, I kindly request you to help me writing a for loop (or any other way) so that the .m file automatically changes the string and reads images one bye one.

I Thank you all in advance...
The STRUCTURE feature, struct, could maybe solve your problem, even though it is not what you specifically asked for. You can use structures to save datasets, like vectors, matrices (images) in an ordered fashion and access them with loops.
Type "help struct" in the terminal, it is pretty easy to understand and use it. Here is an example for images:

% assume that we have some images, lets create 3 random images for this
% example:
imwrite(rand(300,300),'image1','tif')
imwrite(rand(300,300),'image2','tif')
imwrite(rand(300,300),'image3','tif')

image_struct = struct('image',{});

for i = 1:3
end

% now the images are stored in the structure, where it is easy to access
% them, for example to display them in a loop:
for i = 1:3
figure
imshow(image_struct(i).image,[])
end

Hope this is any help, even though I realize it was not what you specifically asked for.

Hi you must follow this method:
i=0;
for x=
i=i+1;
current{i}=x^2;
end
this method works even if x be a vector or a matrix

Bye.