Skip to main content

9.6) Reading And Writing *.MAT Files


  • The "mat" file is the native file format for storing data. It is a sophisticated binary file format, that offers fast saving and loading, as well as fairly efficient use of storage
  • Given that it is heavily supported within Matlab, it is the preferred storage file format for working with data within Matlab. It is a propriety file format and so is not readily readable by other programs
  • We can save the entire workspace:
  • [matlab]
    >> A=randn(20,2);
    >> B=rand(100,100);
    >> save('workspace.mat')
    [/matlab]

  • Or just save specific variables:
  • [matlab]
    >> save('random_data.mat', 'A');
    [/matlab]

  • And we can add data (append) to an existing "mat" file
  • [matlab]
    >> save('random_data.mat', 'B', '-append');
    [/matlab]

  • And importantly, we can load data from a "mat" file:
  • [matlab]
    >> load('workspace.mat')
    [/matlab]

    Or just load specific variables from a "mat" file into the current workspace

    [matlab]
    >> load('workspace.mat', 'A');
    [/matlab]