Say I have a .mat file with several instances of the same structure, each in a different variable name.
I want to process each instance found in a file (which I find using whos('-file' ...
). I was hoping that load
would let me specify the destination name for a variable so that I didn't have to worry about collisions (and so that I didn't have to write self-modifying code a la eval
).
The brute force way to do this appears to be creating a helper function that, using variables with names that hopefully don't conflict with the .mat contents, does something like:
- Does a
whos
on the file to get the contained names. - Iteratively load each contained structure.
- Uses
eval
to assign the loaded structure into, say, a cell array (where one column of the array contains the .mat file's structure names and a corresponding column with the actual contents of each structure from the .mat file).
Is there no more elegant way to accomplish the same thing?
Of course you can! Just use
load
with an output argument.you can
load
the data in a MAT file into a structurethe fields of the structure
ws
will be the variables in the MAT file. You can then even have a cell array of structuresExample