MATLAB - changing variable name with each iteration?

  • MATLAB
  • Thread starter poiuy
  • Start date
  • #1
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.
 

Answers and Replies

  • #2
1,707
5
store the info into a vector that is a function of the counter variable.
 
  • #3
11
0
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
11
0
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
11
0
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
chroot
Staff Emeritus
Science Advisor
Gold Member
10,226
34
Use a two-dimensional vector, i.e. a matrix.

- Warren
 
  • #7
11
0
Thanks, got it now
 
  • #8
3
0
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
26
0
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
1
0


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.

I hope this will help you.

Kind regards
 
  • #12
4
0
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
2
0
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
1
0
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
1
0
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
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, 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')

% 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.
 

Related Threads on MATLAB - changing variable name with each iteration?

Replies
2
Views
12K
  • Last Post
Replies
4
Views
3K
Replies
5
Views
127K
Replies
1
Views
25K
Replies
4
Views
4K
Replies
2
Views
8K
Replies
2
Views
3K
  • Last Post
Replies
1
Views
2K
Replies
0
Views
3K
Top