MATLAB Answers

how to save variables to an array using a for loop?

157 views (last 30 days)
Debby Amaya
Debby Amaya on 29 Jun 2017
Edited: Jan on 30 Jun 2017
If I have m1 = 12, m2 = 34, m3 = 12, m4 = 45 how can I save it in the form of y = [m1, m2, m3, m4] using a for loop? kind of like.. for i = 2:6 y = ?.... Thanks


Stephen Cobeldick
Stephen Cobeldick on 29 Jun 2017
The most important question is: how did you end up with all of those separate variables? The best solution is to avoid creating lots of variables. How? By using just one variable... and luckily it is trivial to avoid when loading/importing data or when creating data, by using indexing or fieldnames. So, given that it is so trivial to avoid this problem, when not improve your code by avoiding this whole problem altogether?
The name MATLAB comes from MATrix LABoratory: MATLAB is most efficient when data is kept together as much as possible (in vectors, in matrices, or in ND arrays) and the data accessed using indexing or by writing vectorized code. Like many other programming languages it is very inefficient to generate or access lots of separate variables.
About the worst solution would be to use eval (which some beginners love to use), you might like to first know what the MATLAB documentation says about it: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
You might also like to read what experienced MATLAB users say about what you are trying to do (hint: they strongly advise against it):
Debby Amaya
Debby Amaya on 30 Jun 2017
I calculated the mean pixel's value for several different images using MATlab, that's how I ended up with different variables. I want to put them together now to graph those values vs. time.
Stephen Cobeldick
Stephen Cobeldick on 30 Jun 2017
"I calculated the mean pixel's value for several different images using MATlab, that's how I ended up with different variables"
So the best and simplest solution is to put that data into one variable when you make those calculations. That is what any experienced user would do, and you can do that too.
Presumably you perform those calculations in a loop: in that case you can simply preallocate the output matrix and use indexing inside the loop to allocate the calculated values. Simple, fast, efficient, neat, easy to understand... there is no reason why you cannot write good code now.

Sign in to comment.

Answers (2)

Jan on 30 Jun 2017
Edited: Jan on 30 Jun 2017
As suggested above: The best idea is not to create the variables in this way at all. Hiding an index in the name of the variable is a bad idea.
But if you have these names already and cannot change the code, which creates them - what's wrong with:
y = [m1, m2, m3, m4]
? Is the number of variables unknown? Do the variables come from an imported MAT file? Then this would help:
Data = load('File.mat');
C = struct2cell(Data);
M = cat(2, C{:});
Nevertheless, the main goal is to avoid the creation of such variables from the beginning.


Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!