Search code examples
matlabshellunixmat

Read .mat files from Unix shell


Is there a way to read .mat files from the UNIX command line shell?

Such as cat mymat.mat ?

I am aware of the possibilities to load it in MATLAB or python, but these are not available for me atm.


Solution

  • GNU Octave may be an option as it can be freely installed without cost.

    Say you ran a session something like this and created two arrays, A and B:

    octave:1> A = [ 1:3; 4:6; 7:9 ];
    octave:2> B = [ 11:13; 14:16; 17:19 ];
    octave:3> save -7 myfile.mat A B
    

    Then, in the shell, outside of Octave, you could do this to see the names of the variables in the file:

    $ octave-cli <<< "who -file myfile.mat"
    

    Sample Output

    Variables in the file myfile.mat:
    
    A  B
    

    And then this to dump the variables:

     $ octave-cli <<< "load myfile.mat;A"
    

    Sample Output

    A =
    
       1   2   3
       4   5   6
       7   8   9
    

    And:

    $ octave-cli <<< "load myfile.mat;B"
    

    Sample Output

    B =
    
       11   12   13
       14   15   16
       17   18   19