Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB - changing variable name with each iteration?

  1. Feb 21, 2008 #1
    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.
  2. jcsd
  3. Feb 21, 2008 #2
    store the info into a vector that is a function of the counter variable.
  4. Feb 21, 2008 #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 ;

    But if each x value was a vector rather than an integer how could I index the different current vectors.
    Last edited: Feb 21, 2008
  5. Feb 21, 2008 #4
    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: Feb 21, 2008
  6. Feb 21, 2008 #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.
  7. Feb 21, 2008 #6


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use a two-dimensional vector, i.e. a matrix.

    - Warren
  8. Feb 21, 2008 #7
    Thanks, got it now
  9. Feb 25, 2008 #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;']);

    You can remove the first ; to have echo.

    I hope this will help you.

    Kind regards
  10. May 28, 2008 #9
    Warren could you explain what was concluded here?
    I am in the same position as the guy who asked this question...
  11. May 29, 2008 #10

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

    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.
  12. Mar 11, 2009 #11
    Re: change a variable name

    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!!!

  13. Aug 9, 2009 #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'])

    What this will do is run a for loop 10 times and create:
    etc. up to

    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)])

    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.
  14. Dec 14, 2009 #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...

    - 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]']);

    - 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: Dec 14, 2009
  15. Apr 6, 2010 #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...
  16. Apr 8, 2010 #15
    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);​


    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
  17. Mar 24, 2011 #16
    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:

    % 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'));

    % 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

    Hope this is any help, even though I realize it was not what you specifically asked for.
  18. Sep 14, 2012 #17
    Hi you must follow this method:
    for x=
    this method works even if x be a vector or a matrix

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: MATLAB - changing variable name with each iteration?