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

22 views (last 30 days)
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

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 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)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!