Matlab load .mat data with nested sturcture

Viewed 43

I need to read a .mat database that contains a structure as below:

Database1.(Condition).(clip_number).data

There are 60 clips in the condition that I need. I have tried data = load('file.mat', 'Database1.Condition1.clip1')

But seems the only valid variable I could access is Database1. So, how could I load the data I need? Please note that I have no experience in Matlab before. Thank you.

1 Answers

When you load a .mat file in Matlab, you can only select the top-level variable(s) to load. It's not like a document or JSON database where you can index into it deeply at load time.

So do this to grab Database1:

s = load('file.mat', 'Database1');

And then grab the individual clips from it:

myClip1 = s.Database1.Condition1.clip1;
myClip2 = s.Database1.Condition1.clip2;
% ... blah blah blah ...

You might want to loop over them:

nClips = 60;
for iClip = 1:nClips
   clipFieldName = sprintf('clip%d', iClip);
   myClip = s.Database1.Condition1.(clipFieldName);
   % ... do stuff with that clip ...
   % Maybe store the results in a cell array like this:
   out{iClip} = whateverYouGotFromProcessing(myClip);
end
Related