필터 지우기
필터 지우기

Using Evalin and a Cell

조회 수: 1 (최근 30일)
B. J.
B. J. 2012년 2월 10일
Hello,
I have a cur_hdr which contains my new variable name I want to write to my workspace. I have a 3x1 cell called 'Temp'. I want my new variable name to equal temp in my workspace.
I've tried a bunch of variations such as
evalin('base',[cur_hdr '(:,1)=' temp ';']);
This gives me
??? Error using ==> horzcat CAT arguments dimensions are not consistent.
When I try something such as x = temp
I don't have a problem. I'm simply trying to reassign variables.
thanks in advance

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 10일
Your expression [cur_hdr '(:,1)=' temp ';'] needs to construct a string, but your variable "temp" is not a string.
For your purposes you should use assignin() instead:
assignin('base', cur_hdr, temp);

추가 답변 (1개)

the cyclist
the cyclist 2012년 2월 10일
So,
[cur_hdr '(:,1)=' temp ';']
should be a string that can be evaluated. With what you have said, I am guessing that is not happening. I think this is because with the syntax you are using, MATLAB is looking into the variable temp, rather than just using the string 'temp' as part of the eval statement.
My advice is to first write the string out by hand:
['x = temp']
Then isolate the part of the string that you want to replace with a variable:
['x',' = temp']
Then, replace the string 'x' with your variable:
y = 'x';
[y,' = temp']
This way, you "reverse engineer" the string that you will evaluate. I find this step-by-step approach avoids a lot of confusion.

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by