How can I programmatically access specific variables that are dynamically loaded?

22 ビュー (過去 30 日間)
Sean
Sean 2011 年 7 月 22 日
コメント済み: Stephen23 2021 年 6 月 28 日
I am trying to load a specific variable and use it in my program. When I do so (using the following code), the data comes in as a struct (e.g. x.out1, x.out2, etc.)
x=load(strcat('Testfiles\', testcase.filename),strcat('out',num2str(num_videos)));
Because the content of x is dynamic, I cannot explicitly state which outN I am looking for in the code.
vid=x.out(...);
Because this is not a string, but a variable assignment, I cannot use strcat('out',num2str(num_videos)).
How can I assign the loaded variable to vid?
Thanks, Sean

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 22 日
Use fieldnames() to get your true variable names then use dynamic field name technique to get the value.
VarNames=fieldnames(x);
MyVarName=VarNames{1};
Value=x.(MyVarName)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by