MATLAB - changing variable name with each iteration?

In summary: How can I address each image individually afterwards?In summary, the conversation is about using MATLAB to collect and plot data for a physics project. The person is using a for loop to collect the data and wants to
  • #1
poiuy
11
0
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.
 
Physics news on Phys.org
  • #2
store the info into a vector that is a function of the counter variable.
 
  • #3
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:
  • #4
ice109 said:
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:
  • #5
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.
 
  • #6
Use a two-dimensional vector, i.e. a matrix.

- Warren
 
  • #7
Thanks, got it now
 
  • #8
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.

I hope this will help you.

Kind regards
 
  • #9
Warren could you explain what was concluded here?
I am in the same position as the guy who asked this question...
 
  • #10
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.
 
  • #11


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!



Karto said:
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.

I hope this will help you.

Kind regards
 
  • #12
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.
 
  • #13
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) '=[0]']);
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...

Many thanks for your help!
 
Last edited:
  • #14
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
image2 = imread('apple_2' , 'tif');
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...
 
  • #15
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 i=1:number_of_tasks
fprintf('\nFor task number %d', i);​
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];
number_of_tasks= 2;

For task 1.

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]

For task 2.

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?? :smile:

P.S. Sorry for the long post
 
  • #16
rihanplys said:
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
image2 = imread('apple_2' , 'tif');
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, let's 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')

% create a structure to where you can load your images
image_struct = struct('image',{});

% read your images into the struct
for i = 1:3
image_struct(i).image = double(imread(sprintf('image%d',i), 'tif'));
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.
 
  • #17
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.
 

1. How do I change a variable name with each iteration in MATLAB?

In order to change a variable name with each iteration in MATLAB, you can use the "eval" function. This function evaluates a string as a MATLAB expression and allows you to dynamically create and assign values to variables.

2. Can I use a loop to change variable names in MATLAB?

Yes, you can use a loop to change variable names in MATLAB. By using the "eval" function within a loop, you can create and assign values to variables with different names each time the loop runs.

3. Is it possible to change the variable name based on the iteration number in MATLAB?

Yes, it is possible to change the variable name based on the iteration number in MATLAB. You can use the "num2str" function to convert the iteration number into a string, which can then be concatenated with the desired variable name using the "eval" function.

4. What is the advantage of changing variable names with each iteration in MATLAB?

The advantage of changing variable names with each iteration in MATLAB is that it allows you to store and access multiple values without overwriting them. This is useful when dealing with large datasets or performing multiple calculations with different input values.

5. Are there any limitations to changing variable names with each iteration in MATLAB?

There are a few limitations to changing variable names with each iteration in MATLAB. One limitation is that it can make the code more complex and difficult to read, especially if there are many variables being created and assigned. Additionally, using the "eval" function can also lead to errors if not used properly.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top