If you are familiar with the MATLAB environment, then you may know that the workspace is not maintained across different sessions of MATLAB. When you quit MATLAB, the workspace clears. However, you can save any or all of the variables in the current workspace to a MAT file. Essentially, MAT files are binary MATLAB files that store workspace variables. You can reuse the workspace variable later during the current MATLAB session by just loading the saved workspace variables.

In this tutorial, we will explain how to open a MAT file and load saved variables in MATLAB. First, we will save some data to the variables, then we will save these to a MAT file. After that, we will try to load these MAT files in the MATLAB environment after clearing the workspace.

Let’s say we have a variable ‘a’, and in the variable ‘a’, we will store some random numbers. For this, we will insert a number that is six, so it will generate a matrix of six cross six. We can see that now a variable with the name ‘a’ is saved in the workspace. Similarly, we will create a variable ‘b’ and will save a matrix of magic 3 in this variable. If we don’t want to show this one, it will not affect in storing into the workspace. When we hit enter, it will save into the workspace. But if we close this MATLAB session, these two variables will be gone.

In order to save these two variables into a MAT file, we will use the ‘save’ command. We will write ‘save’ and then the file name. Let’s say we choose ‘tutorial’, and then we will enter the extension that is MAT. After that, we want to save both these two variables, so we enter ‘a’ and then ‘b’. When we enter this one, it will save a ‘tutorial.mat’ file having these two variables into our grant folder.

After that, we will clear all the variables in our workspace. In order to show that these variables stored into the ‘tutorial.mat’ file, we are going to load these two variables by loading ‘load tutorial.mat’. This will load the ‘tutorial.mat’ file and it will contain all the variables in it. As we can see, the ‘a’ and ‘b’ variables appeared in the workspace.

We can also load a single variable into the workspace by using the load command and entering the braces and the structure described in the load command. For example, if we want to load the ‘b’ variable, we will write ‘load tutorial.mat b’. When we load the variable, ‘b’ will appear in the workspace.

If we want to change this variable, we can replace the magic matrix of 3 by a magic matrix of 4. We will choose ‘b’ is equal to magic of 4. When we hit enter, the ‘b’ variable changes to a 4 by 4 matrix. We can save the changed variable into the same MAT file by using the save command and the structure described in the load command. We will use ‘tutorial.mat’ and write the hyphen and write ‘append’. This will replace the variable stored earlier with the same variable name and replace the values of this. In the third argument, we will write ‘b’. When we load again, it will show us the updated variable ‘b’.

In conclusion, working with MAT files in MATLAB can be a useful tool for saving and reusing variables. By following these simple steps, you can easily save and load variables in your MATLAB workspace.