1

For example, imagine that I've in my workspace 100 variables whoes names range from var_1 to var_100. Now I want to create a matrix using all the variable starting with the name 'var_'. I know I could try to list all the variables, but that would be ineficient and long:

A = [var_1 var_2 ... var_100]

Is there a better way to do complish this?

mat
  • 2,412
  • 5
  • 31
  • 69
  • 2
    Don't create 100 variables. Either create a `m x n x 100` matrix if all `var_i` have the same size. Otherwise create a cell array. If you want to do what you are doing, then check out `eval`. – Autonomous Jun 10 '15 at 02:42
  • I'm not sure I made myself clear. The main problem here is that I don't know how to list and manipulate all the variable starting with a specified variable name. – mat Jun 10 '15 at 02:44
  • 1
    Do you control the code that creates those variables? It would be best to avoid having these variables in the first place … – zeeMonkeez Jun 10 '15 at 04:43
  • I don't control it. It's just an example. – mat Jun 10 '15 at 05:33
  • 2
    Never, ever, never, never use this kind of naming. It's bad, very bad. You need `eval` to deal with those kind of variables, and `eval` breaks all things related to MATLAB in terms of performance, see [this answer](http://stackoverflow.com/a/32467170/5211833) on why this is a very bad habit in MATLAB programming. My suggestion would be to transform it once to either a matrix or a cell array and continue from there, not leaving the `eval` in the system. – Adriaan Jan 13 '16 at 23:49
  • 1
    @Adriaan, totally agreed. Sometimes you have to deal with files created by brain-dead code (or brain-dead people), but if so, use these to answers to convert their output *once*, then go forth and sin no more… – Matt Krause Jan 14 '16 at 01:26

2 Answers2

0

While this is probably (most definitely) not a good idea, here is what you can do:

varstr = 'A = ['
for ii = 1:100
    varstr = [varstr, ' var_', num2str(ii)];
end
varstr = [varstr, '];']
eval(varstr)
zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
0

The eval function lets you execute a string of matlab code. Therefore, you just need to generate a string that constructs the array, then pass it to eval. Fortunately, this is pretty easy:

eval([ 'A = [' sprintf('var_%d, ', 1:100) ']']);

It might be a little clearer if you actually do the assignment outside of the eval, like so

A = eval([ '[' sprintf('var_%d, ', 1:100) ']']);

Note that the string here has an extra trailing comma. That doesn't seem to be a problem, at least on 2014b.

If you don't know the total number of variables, you can get them with the who command. This returns a cell array of strings, which you'd then reformat and pass to eval, like so

my_variables = who('var_*');
% You may want to sort (re-order, take a subset of) my_variables here

str_to_run = '['
for ii=1:length(my_variables)
    str_to_run = [str_to_run, my_variables{ii}, ',']
end
Matt Krause
  • 1,113
  • 14
  • 31
  • Note, however, that variables in `my_variables` are probably not in the order you expect, as the strings are not sorted naturally (i.e. they are sorted `var_1, var_10, var_100, var_11` etc.). Use `sort_nat` (http://www.mathworks.com/matlabcentral/fileexchange/10959-sort-nat--natural-order-sort) for this task – zeeMonkeez Jun 10 '15 at 04:04
  • That's true. One of many, many reasons to avoid doing this :-) – Matt Krause Jun 10 '15 at 04:26
  • All other reasons to avoid doing this are mentioned [here](http://stackoverflow.com/questions/30746121/create-a-matrix-from-all-the-variable-in-the-workspace-starting-with-a-specified#comment57303495_30746121) – Adriaan Jan 13 '16 at 23:51