help with load command: how to use data set without knowing name prior to loading

Hi,
When I load a .mat file, a data set get loaded into my session. If I do a 'whos' I can see the name of the data set, or sets.
My question is, is it possible to assign or use this data set by name, without knowing it prior to loading it and checking it with 'whos'.
I hope that makes sense, I am grateful for any help.
Thank you, Scott

 Accepted Answer

file = 'f.mat';
S = load(file);
fn = fieldnames(S); %assuming one variable
data = S.(fn{1})
Load the file into a structure and extract the variable from it using dynamic fieldnames.
Note: I would recommend trying to get a consistent variable name.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!