Convert List of Variables to Structure

3 views (last 30 days)
Good day. Our data is stored in a .mat file under a single structure. I'm attempting to optimize some of our data mining techniques and rather than load the whole .mat desire to load only specific variables. I'm already in the process of converting our single structure files to multiple structures so I can execute:
vars = {'first_var' 'second_var' 'n_var'};
load(target_file, vars{:});
This significantly speeds up the loading process but now I'm trying to figure out a way to avoid changing all our functions. I'd like to, after loading the desired variables, package them back into a single top level structure (named the same as the original, so I can pass that variable around to my functions without any functional changes). I've found several functions that assist with this but none that would simply take a list of variables (vars, in this example) and bundle it back into a structure with the same variables names. Example:
vars = {'first_var' 'second_var' 'n_var'};
load(target_file, vars{:});
Then execute some code so that this:
who
Your variables are:
first_var second_var n_var
target_file vars
Becomes this:
who
Your variables are:
top vars target_file
Where:
top =
first_var: {1x1 cell}
second_var: [1 x 4 double]
n_var: 'some string'

Accepted Answer

Matt J
Matt J on 8 Oct 2013
Edited: Matt J on 8 Oct 2013
This can be done just by calling load() with an output argument,
top=load(target_file, vars{:});
In fact, it's somewhat dangerous practice to do otherwise.
  1 Comment
Matt J
Matt J on 8 Oct 2013
Thomas said:
Well that's much simpler than what I was working toward. Thank you very much! I'll send an update if there are unexpected consequences.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!