Matlab loads a mat into a variable

When loading data from a .Mat file directly into a variable, it saves the structure instead of the variable itself.

Example:

myData.mat contains var1 , var2 , var3

if:

 load myData.mat 

it will create the var1, var2 and var3 variables in my workspace. OK

If I assign that load returns to a variable, it preserves the structure. This is normal since I am loading a few variables.

 foo = load('myData.mat') foo = struct with fields: var1 var2 var3 

However, suppose I am only interested in var1 , and I want to store it directly in the variable foo .

The load has the ability to load only certain variables from the .mat file, but it still retains the structure

 foo = load('myData.mat', 'var1') foo = struct with fields: var1 

I want var1 be directly assigned to foo .

Of course I can do:

 foo = load('myData.mat', 'var1') foo = foo.var1; 

But this should be a way to do this automatically in one line right?

+5
source share
2 answers

load does not behave this way, otherwise load will behave inconsistently depending on the number of variables you requested, which will lead to extremely confusing behavior.

To illustrate this, imagine that you wrote a generic program that wanted to load all the variables from a .mat file, make some changes to them, and then save them again. You want this program to work with any file, so some files may have one variable, and some may have several variables stored in them.

If load used the behavior you specified, you will need to add all kinds of logic to check how many variables were saved in the file before loading and changing.

Here is what this program would look like with the current load behavior

 function modifymyfile(filename) data = load(filename); fields = fieldnames(data); for k = 1:numel(fields) data.(fields{k}) = modify(data.(fields{k})); end save(filename, '-struct', 'data') end 

If the behavior was as you see fit

 function modifymyfile(filename) % Use a matfile to determine the number of variables vars = whos(matfile(filename)); % If there is only one variable if numel(vars) == 1 % Assign that variable (have to use eval) tmp = load(filename, vars(1).name); tmp = modify(tmp); % Now to save it again, you have to use eval to reassign eval([vars(1).name, '= tmp;']); % Now resave save(filename, vars(1).name); else data = load(filename); fields = fieldnames(data); for k = 1:numel(fields) data.(fields{k}) = modify(data.(fields{k})); end save(filename, '-struct', 'data'); end end 

I will leave this to the reader to decide which one is more legible and reliable.

The best way to do what you are trying to do is exactly what you showed in your question. Just reassign the value after loading

 data = load('myfile.mat', 'var1'); data = data.var1; 

Update

Even if you want the variable not to be bound to the struct when the variable has been explicitly specified, you would still encounter inconsistent behavior that would make it more difficult if my program accepted the list of variables to change as an array of cells

 variables = {'var1', 'var2'} data = load(filename, variables{:}); % Would yield a struct variables = {'var1'}; data = load(filename, variables{:}); % Would not yield a struct 
+1
source

It looks ugly, but does what you want:

 foo = subsref(matfile('myData.mat'),struct('type','.','subs','var1')) 

Using matfile allows matfile to partially load variables into memory, that is, it loads only what is needed. The subsref function does the work of the index operator "." in this case.

0
source

Source: https://habr.com/ru/post/1263870/


All Articles