Effacer les filtres
Effacer les filtres

Using Evalin and a Cell

1 vue (au cours des 30 derniers jours)
B. J.
B. J. le 10 Fév 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

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Fév 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);

Plus de réponses (1)

the cyclist
the cyclist le 10 Fév 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.

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by