MATLAB: Getting field names to use in script

fileidMATLAB

Hello,
I want to make a script to automate my data analysis in MATLAB. My files have unique names, and each file contains 7 or so variables with unique names that are 1×1 structures. Each structure has fields with common names, such as "times" and "values". I want to reference the UNIQUEFILE's uniquestructure.values in the script but I don't know how to go about doing this. I only need to load one file at a time for analysis, so it seems that I should use a command to get the name of the unique structure names and then use their fileIDs into later commands, but I haven't found a method that works yet.
I'm obviously pretty new to MATLAB; clear answers are appreciated!

Best Answer

If I understood correctly, this is what you want:
vars = load('somefile.mat'); %load all variables in somefile.mat into structure vars
for varname = fieldnames(vars)' %iterate over the variable
var = vars.(varname{1}); %get content of variable using dynamic field name
if isfield(var, 'values') %check that var has a field called 'values'
varvalue = var.values;
%do something with varvalue
end
end