MATLAB MAT File

  1. Create a MAT File Using the matfile() Function in MATLAB
  2. Save Data in the MAT File Using the save() Function in MATLAB
  3. Load Data From the MAT File Using the load() Function in MATLAB

In this tutorial, we will discuss how to use the MAT file to store and load data in MATLAB.

Create a MAT File Using the matfile() Function in MATLAB

You can create a MAT file with a specific name using the matfile() function. You have to pass the file name and the file extension in the function to create it. For example, let’s create a MAT-file with a file name fileName and .mat extension using the matfile() function. See the code below.

clc
mat_file = matfile('fileName.mat')

You can also enable or disable the write access of the MAT file using the matfile() function. For example, see the code below.

clc
mat_file = matfile('fileName.mat','Writable',true)

In the above code, we gave the write access to the file as true, which means we can save data in the, if the write access is false, then we cannot save data into the file.

Save Data in the MAT File Using the save() Function in MATLAB

You can save data of any type in the MAT file using the save() function. For example, let’s save a matrix and a cell in the MAT file created above. See the code below.

clc
myMatrix = [1 2 3; 4 5 6];
myCell = {'a','b','c'};
mat_file = matfile('fileName.mat','Writable',true);
save('fileName.mat','myMatrix')
mat_file.myCell = myCell;

In the above code, we saved myMatrix in the MAT file fileName.mat using the save() function. We also saved myCell in the MAT file fileName.mat using its object mat_file. If you save data using the save() function, it will overwrite the existing data, but if you use the object of the file, it will not change the previous data and will be stored separately in the MAT file. Note that the MAT file will be saved in the current directory of the .m file. If you want to change the directory, you have to create a new .m file in that specific file and then save the MAT file there.

Load Data From the MAT File Using the load() Function in MATLAB

You can load data from the MAT file using the load() function. For example, let’s load data from the MAT file created above. See the code below.

clc
data = load('fileName.mat')

In the above code, we loaded the MAT file fileName.mat using the load() function. Make sure the file is present in the same directory as the .m file; otherwise, MATLAB will give an error. To load a MAT file from a different directory then you have to pass the full file path along with its name and extension to load it. For example, see the code below.

clc
data = load('C:\myFolder\myFile.mat')

You can check the variable’s information like variable type and size stored in the loaded MAT-file using the whos function. For example, let’s check the variables stored in the above MAT-file fileName.mat. See the code below.

clc
whos('-file','fileName.mat')

Output:

  Name          Size            Bytes  Class     Attributes

  myCell        1x3               318  cell                
  myMatrix      2x3                48  double  

As you can see in the output, the MAT-file fileName.mat has a cell array and a matrix stored inside it. If you want to extract and print only one variable from the MAT file, you can use that variable’s name to extract it. For example, let’s extract the variable myCell from the above MAT file. See the code below.

clc
data = load('fileName.mat');
mat = data.myMatrix

Output:

mat =

     1     2     3
     4     5     6

As you can see in the output, we have extracted the required variable from the MAT file and store it in the variable mat. Check this link for more information about the MAT file.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.