Filter löschen
Filter löschen

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

19 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 22 Jul. 2011
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)

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by