Using Evalin and a Cell
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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);
0 个评论
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!