List values of variables with wildcard

8 ビュー (過去 30 日間)
Greg
Greg 2016 年 2 月 9 日
編集済み: Stephen23 2017 年 5 月 4 日
Is there a command or other simple way to list the values of variables specified by a wildcard? For example, if I type
>> command a*
MATLAB would give me the names and values of all variables that start with "a". (who and whos do not give the values.) Thanks. Greg Reese
  1 件のコメント
Stephen23
Stephen23 2016 年 2 月 9 日
編集済み: Stephen23 2016 年 2 月 9 日
A much better solution would be to stop writing buggy, slow and obfuscated programming that accesses variable names dynamically (see Guillaume's answer), and to switch to using a structure. Then you can easily access all of its fieldnames, filter them, sort them, or do whatever is required. Much easier:
Here is why accessing variable names dynamically is a bad idea ("iffy", as Guillaume said), although beginners seem to love doing this regardless of the fact it creates slow, buggy code and always ignore our advice to use better programming methods:

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

採用された回答

Guillaume
Guillaume 2016 年 2 月 9 日
編集済み: Guillaume 2016 年 2 月 9 日
What you're trying to do sounds iffy. You should not be manipulating data by their variable names. Saying that, it's very easy but it involves eval which should raise some alarm bells:
varnames = who('a*');
values = cellfun(@eval, varnames, 'UniformOutput', false);
cell2table([varnames, values], 'VariableNames', {'Variable', 'Value'})
  4 件のコメント
Aditya Asopa
Aditya Asopa 2017 年 5 月 3 日
Thanks Guillaume, Sometimes the variable names are in not our hands. I need to process a file from which I need to pick columns as variables. All column headers are names as A1,A2, A3 and so on.
Could you suggest a possible way out?
Thanks a ton - Adi
Stephen23
Stephen23 2017 年 5 月 4 日
編集済み: Stephen23 2017 年 5 月 4 日

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by