Create variables with names from strings

66,591

Solution 1

You can do it with eval but you really should not

eval(['x', num2str(i), ' = ', num2str(i)]); %//Not recommended

Rather use a cell array:

x{i} = i

Solution 2

I also strongly advise using a cell array or a struct for such cases. I think it will even give you some performance boost.

If you really need to do so Dan told how to. But I would also like to point to the genvarname function. It will make sure your string is a valid variable name.

EDIT: genvarname is part of core matlab and not of the statistics toolbox

Solution 3

for k=1:10
   assignin('base', ['x' num2str(k)], k)
end

Solution 4

Although it is long overdue, i justed wanted to add another answer.

the function genvarname is exactly for these cases

and if you use it with a tmp structure array you do not need the eval cmd

the example 4 from this link is how to do it http://www.mathworks.co.uk/help/matlab/ref/genvarname.html

 for k = 1:5
   t = clock;
   pause(uint8(rand * 10));
   v = genvarname('time_elapsed', who);
   eval([v ' = etime(clock,t)'])
   end

all the best

eyal

Solution 5

If anyone else is interested, the correct syntax from Dan's answer would be:

eval(['x', num2str(i), ' = ', num2str(i)]);

My question already contained the wrong syntax, so it's my fault.

Share:
66,591
Potaito
Author by

Potaito

Updated on February 27, 2020

Comments

  • Potaito
    Potaito about 4 years

    Let's assume that I want to create 10 variables which would look like this:

    x1 = 1;
    x2 = 2;
    x3 = 3;
    x4 = 4;
    .
    .
    xi = i;
    

    This is a simplified version of what I'm intending to do. Basically I just want so save code lines by creating these variables in an automated way. Is there the possibility to construct a variable name in Matlab? The pattern in my example would be ["x", num2str(i)]. But I cant find a way to create a variable with that name.

  • Potaito
    Potaito about 11 years
    Wow thanks, very simple. And yes, I will use cells but also this method you presented here ;)
  • Potaito
    Potaito about 11 years
    Thanks. I will use arrays instead of cells. The example in my question was just very simple in order to have an easy question for the information I'm looking for. What I'm actually doing isn't as naive as my question :p
  • Dan
    Dan about 11 years
    In future, you can actually edit my answer if there are small mistakes like this. There is an edit link at the bottom. If you don't have enough reputation you can also just leave a comment and someone else will make the edit for you. I've fixed mine now ;)
  • Amro
    Amro about 11 years
    @bdecaf: btw genvarname is part of core MATLAB
  • Oleg
    Oleg about 11 years
    I really discourage you from using eval to pop incremental variables in your workspace. If you want a name create a structure: s.(sprintf('x%d', i)) = i;
  • bdecaf
    bdecaf about 11 years
    oh my bad. I could have sworn it was part of that toolbox.
  • Flyto
    Flyto over 9 years
    suggest changing 'base' to 'caller'. That way, AIUI, it ought to work anywhere where somebody is not paying specific attention to workspaces.
  • Charlie Parker
    Charlie Parker about 8 years
    why is this not recommended?
  • Dan
    Dan about 8 years
    @CharlieParker It makes your code hard to read (just compare my two solutions) and forces you to use eval again to iterate through your variables which is unnecessarily complicated. It makes your code very difficult to debug and adds in more room for error. Think about how you would find the largest x in the two solutions as an example of how complicated it's going to be if you use eval! Also it clutters your workspace with all those variables which you don't need. This is what arrays exist for! It is a sloppy, lazy way yo code that shows you didn't take the time to plan properly.
  • pattivacek
    pattivacek almost 8 years
    @CharlieParker eval is also fairly slow because the optimizer can't know what it's going to do.
  • Dan
    Dan almost 8 years
    @patrickvacek that's a great point. Do you have a link to a test demonstrating this maybe?
  • pattivacek
    pattivacek almost 8 years
    @Dan not readily available, but the idea is straight from the source: mathworks.com/help/matlab/matlab_prog/…