MATLAB Answers

Variable names in loop

48 views (last 30 days)
Rens Jochemsen
Rens Jochemsen on 24 May 2017
Commented: Jan on 24 May 2017
hi !
I want to create variable names in a loop, so A1, A2, A3 -- AN
something like
for i=1:N
I googled for it but only find answers from people that discourage you to use it, and use indexing instead. However, indexing is not quite an option for me, Because the A are already matrices.
I have a code that works, but at the moment, every line I have to repeat 5 times, for every dataset
I just want one forloop
for i=1:5
data_norm_i = data_i./max(data_i)
I saw eval is proposed but i don't fully understand how to use it.
thanx in advance !


Adam on 24 May 2017
This is exactly what indexing is for. Nothing in what you have said seems to support you saying it is not an option.
Use a cell array if your 'whatever' is non-scalar, or use a higher-dimensional array if they are all the same size.
for i=1:5
data_norm{i} = data{i}./max(data{i})
Stephen Cobeldick
Stephen Cobeldick on 24 May 2017
"indexing is not quite an option for me, Because the A are already matrices"
How did you generate those matrices?
The best solution is to use indexing. Rather than putting pseudo-indexing into the variable names (as you have done), your code will be much simpler, faster, more efficient, neater, expandable, generalized, etc, etc, when you simply use real indices. That is the best solution. It is what we all do (and as a result we never have the exact problem that you have now: simply by making better design decisions, we avoid having to write bad code).
Perhaps while you "googled for it" you also found the MATLAB documentation, which clearly states that what you want to do is not recommended: "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."
It is amazing how many beginners claim to have the only use case ever that absolutely requires doing this, and that they know better than MATLAB's own documentation and the advice of many experienced programmers:
The documentation and those more experienced users want you/everyone to write good code. You might like to consider their advice.
Jan on 24 May 2017
@Rens: You noticed that you find only answers which suggest not to create variables dynamically with an index hidden in the name. You find many many many of these answers. They contain sentences like: "Don't do this" and "there is always a better solution". And then you insist, that indexing is not an option for you.
We have written hundrets of answers concerning this problem. And the answers are always the same. The problem does not concern Matlab only, but all programming languages, which allow to create variables dynamically. Remember that there are many languages, which do not allow this and of course you can solve all problems with them also.
Please, take the time to think twice. Many very experienced programmers tell you, that the only valuable and reliable option is not to do this. So please do your salf the favor and trust us. Hiding indices in the names of variables is a shot in your knee.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!