필터 지우기
필터 지우기

Use eval statement to create Matrices with variables

조회 수: 11 (최근 30일)
Brian
Brian 2012년 10월 3일
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

채택된 답변

Jan
Jan 2012년 10월 3일
편집: Jan 2012년 10월 3일
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])
See:
['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;
  댓글 수: 3
Brian
Brian 2012년 10월 3일
So it looks like you'd recommend using a structure storing each of the variables rather than creating separate variables with the eval statement. I can play around with that, although I'm not great with structures.
The eval statement still doesn't work. Gives me "Index Exceeds matrix dimensions" error.
Thanks Jan
Jan
Jan 2012년 10월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 10월 3일
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?
  댓글 수: 2
Matt Fig
Matt Fig 2012년 10월 3일
Brian comments:
"This is part of a loop that is being done numerous times. I'd like each iteration in my nested loop to create a variable with the name of the combination that I'm on for further analysis. The variables SL_Per and Rtn_Len each have 3 different values. In the end I'd like 9 SL_Excess_Fwd variables with different names and storing the result of that iteration.
All that said, if you have a better suggestion I'd love to hear it. I hate trying to use the eval function. Speed isn't an issue because I'm only doing this 9-15 times total in my program.
Thanks, Brian"
Matt Fig
Matt Fig 2012년 10월 3일
편집: Matt Fig 2012년 10월 3일
It is common to use structures or cell arrays instead.
for ii = 1:4
SL_Per{ii} = rand(randi(10,1,2));
end
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));
end
end
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by