Calling a variable in Matlab without using the full name?

78 views Asked by At

I am creating a script to be universal in opening pre-made Data files that hold the same basic information, but the original files are not consistent in how they are named.

An example, the same variable has 3 different variants in the different files:

Data01.SubData

Data01.SubData_01

Data01.SubData01

Is there a way to ask Matlab to look for and call any variables that start with "Data01.SubData" regardless of the the final characters in the name? Any help is appreciated.

1

There are 1 answers

0
Peter On BEST ANSWER

Sure, you can use fieldnames to get a list of names, do your matching, then grab the field you want:

f = fieldnames(Data01);
match = regexp(f, '^SubData.*');
fieldnum = find(~cellfun(@isempty, match));
subdata = Data01.(f{fieldnum});

If the confusion is at the top level rather than at the substruct level, you can do the same thing by loading your .mat file into another struct, rather than dumping the variables directly into the workspace: mydata = load('somefile.mat');