Use eval statement to create Matrices with variables

15 views (last 30 days)
Brian on 3 Oct 2012
I am having trouble with some seemingly basic eval syntax. I have a few variables in loops that I'd like to use to create the name of a new variable and set it equal to the result of my loop.
Here's where I've gotten so far.
eval(['SL_Excess_Fwd' '_' num2str(RtnLen(rl)) '_SL_' num2str(SL_Per(slp)) '=' SL_Excess_Fwd ]);
Gives me the error - Warning: Out of range or non-integer values truncated during conversion to character.
Error using horzcat
CAT arguments dimensions are not consistent.
When I do just the following I get the answer = SL_Excess_Fwd_5_SL_2
['SL_Excess_Fwd' '_' num2str(RtnLen(rl)) '_SL_' num2str(SL_Per(slp))]
I basically just want to simulate the command SL_Excess_Fwd_5_SL_2 = SL_Excess_Fwd;
Thanks in advance for the help, Brian

Accepted Answer

Jan on 3 Oct 2012
Edited: Jan on 3 Oct 2012
What is "SL_Excess_Fwd"? If it is not a string, but contains non-integer floating point values, the conversion fails here with the posted error message:
Name = ['SL_Excess_Fwd' '_' num2str(RtnLen(rl)) '_SL_' num2str(SL_Per(slp))];
eval([Name, '=', SL_Excess_Fwd])
['string', -1]
Here the -1 cannot be converted to a CHAR value unequivocally.
I think you want:
eval([Name, '=', 'SL_Excess_Fwd']) % Quotes around the variable!
As Matt Fig has already mention, such problems are typical, when you use EVAL. It would be much smarter to use an index as index, instead of hiding it in the name of the variable:
SL_Excess_Fwd(RtnLen(rl)).SL(SL_Per(slp)) = SL_Excess_Fwd
Or at least a dynamic fieldname:
SL_Excess.sprintf('Fwd%d', RtnLen(rl)).sprintf('SL%d', SL_Per(slp)) = SL_Excess_FWD;
Jan on 3 Oct 2012
It is better to learn how to use structs, than to use the evil EVAL to hide indices in the names of variables.
Which EVAL statement produces the "Index exceeds matrix dimensions" error? The command you've posted in the comment, is strange:
eval([num2str(RtnLen(rl)) '_SL_' num2str(SL_Per(slp)) '=' SL_Excess_Fwd])
There are still no quotes around SL_Excess_Fwd. And the name of the variable you want to assign starts with a number now.
Please, Brian, stop this EVAL stuff. As you see this indirect programming technique causes too much troubles.

Sign in to comment.

More Answers (1)

Matt Fig
Matt Fig on 3 Oct 2012
Why are you using EVAL in the first place? This is usually strongly discouraged because it is slow, it is prone to bugs and strange behavior, and it makes the code hard to handle and very difficult to understand. So what is it you think you gain by using EVAL instead of using standard MATLAB variable structures?
Matt Fig
Matt Fig on 3 Oct 2012
It is common to use structures or cell arrays instead.
for ii = 1:4
SL_Per{ii} = rand(randi(10,1,2));
Now SL_Per is a cell array, and the kth element corresponds to the iith iteration. You access this like:
SL_Per % Look at the cell array.
SL_Per{3} % Look at the third element.
Or one could use a structure:
for ii = 1:3
for jj = 1:2
S.(sprintf('%s%i','run',ii)){jj} = rand(randi(10,1,2));
Now S is a structure. Have a look:
S % Look at the fieldnames of the structure
S.run3 % Look at the contents of field run3
S.run2{2} % Look at the second element of run2
If you have a scalar result from each run instead of an array, you could use regular arrays instead of cell arrays.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!