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

 採用された回答

Jos (10584)
Jos (10584) 2011 年 2 月 18 日

0 投票

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 件のコメント

Oleg Komarov
Oleg Komarov 2011 年 2 月 18 日
Evil
the cyclist
the cyclist 2011 年 2 月 18 日
But a necessary evil? These seems one of the few times that eval is truly needed!
Oleg Komarov
Oleg Komarov 2011 年 2 月 18 日
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 2011 年 2 月 18 日
That works perfectly, thank you! On this occasion I'm more than happy to use Evil for good...
Walter Roberson
Walter Roberson 2011 年 2 月 18 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by