フィルターのクリア

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 Center および File ExchangeDebugging and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by