Adding arrays found using 'who' function

Hello,
I have used the 'who' function to obtain the required 2D numeric arrays from a list of variables, like this;
Find_Target_Vars = who ('Target_Str')
Find_Target_Vars =
'A'
'B'
'C'
I now need to create a new (numeric,double) variable D, so that D = A + B + C.
How can I get from the list of variables in the cell array, Find_Target_Vars, to adding the actual listed variables together?
Many thanks, Iain

 Réponse acceptée

Jos (10584)
Jos (10584) le 18 Fév 2011
This is one of the (rare) cases EVAL comes in handy.
evalStr = sprintf('%s+', Find_Target_Vars{:}) ;
evalStr = evalStr(1:end-1) ; % remove trailing +
D = eval(evalStr)
Jos

5 commentaires

Oleg Komarov
Oleg Komarov le 18 Fév 2011
Evil
the cyclist
the cyclist le 18 Fév 2011
But a necessary evil? These seems one of the few times that eval is truly needed!
Oleg Komarov
Oleg Komarov le 18 Fév 2011
Depends how A, B and C were generated...if a cell array of a struct were unavailable then probably eval is the only way.
Iain
Iain le 18 Fév 2011
That works perfectly, thank you! On this occasion I'm more than happy to use Evil for good...
Walter Roberson
Walter Roberson le 18 Fév 2011
If eval is the answer, then you are probably asking the wrong question.
If you are having to examine the workspace to figure out what variables are there, then you are probably doing something that there are better ways to do -- something like having used load() to poof variables into the workspace.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by