Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab load mat into variable

Tags:

matlab

load

When loading data from a .Mat file directly into a variable, it stores an struct instead of the variable itself.

Example:

myData.mat contains var1, var2, var3

if I do:

load myData.mat

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

If I assign what load returns to a variable, it stores an struct. This is normal since I'm loading several variables.

foo = load('myData.mat')

foo = 
  struct with fields:
    var1
    var2
    var3

However suppose that I'm only interested in var1 and I want to directly store into a variable foo.

Load has an option of loading only specific variables from a .mat file, however it still stores an struct

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

I want var1 to be directly assigned to foo.

Of course I can do:

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

But it should be a way of doing this automatically in one line right?

like image 889
Sembei Norimaki Avatar asked Feb 06 '17 10:02

Sembei Norimaki


People also ask

How do you load a MAT file to a variable in MATLAB?

load( filename ) loads data from filename . If filename is a MAT-file, then load(filename) loads variables in the MAT-file into the MATLAB® workspace. If filename is an ASCII file, then load(filename) creates a double-precision array containing data from the file.

How do you load a workspace in MATLAB?

Open the Workspace Browser MATLAB Toolstrip: On the Home tab, in the Environment section, click Layout. Then, in the Show section, select Workspace. MATLAB command prompt: Enter workspace .

How do you load a dataset in MATLAB?

Open the Import Tool MATLAB® Toolstrip: On the Home tab, in the Variable section, click Import Data. MATLAB command prompt: Enter uiimport( filename ) , where filename is a character vector specifying the name of a text or spreadsheet file.

How do I save a .MAT file in MATLAB?

Select MATLAB > General > MAT-Files and then choose a MAT-file save format option.


2 Answers

If the MAT-file contains one variable, use

x = importdata(mat_file_name)
like image 185
Fuad Numan Avatar answered Oct 01 '22 20:10

Fuad Numan


load does not behave this way otherwise load would behave inconsistently depending upon the number of variables that you have requested which would lead to an extremely confusing behavior.

To illustrate this, imagine that you wrote a general program that wanted to load all variables from a .mat file, make some modification 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 multiple variables stored in them.

If load used the behavior you've specified, then you'd have to add in all sorts of logic to check how many variables were stored in a file before loading and modifying it.

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

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 the way that you think you want

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'll leave it to the reader to decide which of these is more legible and robust.

The best way to do what you're trying to do is exactly what you've shown in your question. Simply reassign the value after loading

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

Update

Even if you only wanted the variable to not be assigned to a struct when a variable was explicitly specified, you'd still end up with inconsistent behavior which would make it difficult if my program accepted a list of variables to change as a cell array

variables = {'var1', 'var2'}

data = load(filename, variables{:});    % Would yield a struct

variables = {'var1'};

data = load(filename, variables{:});    % Would not yield a struct
like image 45
Suever Avatar answered Oct 01 '22 21:10

Suever