Clear Filters
Clear Filters

Using Evalin and a Cell

1 view (last 30 days)
B. J.
B. J. on 10 Feb 2012
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

Accepted Answer

Walter Roberson
Walter Roberson on 10 Feb 2012
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);

More Answers (1)

the cyclist
the cyclist on 10 Feb 2012
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.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!