how to convert fields of a struct into numbered matrices

1 view (last 30 days)
hello, I am trying to convert the fields of a 15X1 struct into numbered arrays from 1 to 15. I want to store every single field in different matrix in order to access it by just calling the name of the corresponding matrix e.g N1(name of the matrix) for the 1st field, N2 for the second field etc... I do not know if this applicable. Is there any way of doing it? And after all is there any point of doing so?
thank you in advance

Accepted Answer

Orion
Orion on 7 Nov 2014
Edited: Orion on 7 Nov 2014
Hi,
you can create variables using eval
% create the variable N1,...,N5
for i = 1:5
eval(['N' num2str(i) '=rand();']);
end
you can do this with any kind of expression.
BUT : the use of eval is dangerous, because when you debug, you will not find the name of the variables. Meaning, if you search the variable N4 with the find files , you will never find it, beacause there is no line like N4 = ...
then, if the expression used in eval is long and/or complicated, it will be very tricky to debug.
Also, if you plan to deploy your application, with the compiler, eval could not work in the standalone.
On the bright side, you can use it from time to time for small script when you want to work fast and you are the only one to use it. That's the way I use it.
For your problem, you can use it, with the appropriate names and the correct syntax, it's up to you.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!