Search code examples
matlabfile-iostructoctavemat-file

convert struct type to matrix octave


I have a myfile.mat that contains 3000x35 size data. When I load it into a variable as :

a = load('myfile.mat')

It gives struct of size 1x1 . How can I get exact form as matrix. This is required because I need to change certain column values.


Solution

  • (This answer is valid for MATLAB, I am not sure it works exactly the same way in Octave)


    You have several options:

    Option 1:

    If the .mat file only contains one variable, you can do:

    a = struct2array(load('myfile.mat'));     % MATLAB syntax
    a = [struct2cell(load('myfile.mat')){:}]; % Octave syntax
    

    Option 2:

    You need to know the name of the variable at the time of saving, which is now a name of a field in this struct. Then you would access it using:

    a = load('myfile.mat'); a = a.data;
    

    Option 3 (unrecommended!):

    Just remove the a = part of the expression,

    load('myfile.mat');
    

    Then the variables inside this file will "magically spawn" in your workspace. This is not recommended because it makes it difficult (impossible?) to see when certain variables are created.