The Workspace

As it was previously looked at in Introducing MATLAB, the workspace contains all the variables that have been stored by MATLAB. The workspace is temporary; once you shut MATLAB, if you have not saved your code or your workspace, all your variables will be lost. You can do a few things with the workspace, including:

  • Adding or Saving variables
  • Editing variables
  • Deleting variables

You can also back up or clear the workspace by right clicking within the workspace window and selecting your preferred options. You can alternatively click on ‘Save Workspace’ under the ‘Variable’ section of the ‘Home’ tab. When you save your workspace, you can easily import all your variables back in future settings by calling the .mat file that you saved.

Let’s consider an example.

 

Example 2.1
You have just finished one of the most time-consuming part of a MATLAB project, which has taken you hours to complete. You call it a day and wish to shut down your computer. Your workspace has more than 100 variables and you need to use them when you continue working on your project tomorrow. What do you do?

You can do a few things. If you were intelligent with your coding, you would have typed all your code into script files, and not in the command window. This means that most, if not all, of the variables can be recreated by running the script files again. The only variables that can’t be recreated would be those that you manually created using the command window. You may think that it would be too time consuming to run tens of scripts to recreate your variables, and you are right to think that. To avoid this problem, you could create a “controlling script” that calls all the other scripts that are relevant, and what this achieves is the automated running of all the script, which in turn calls all the variables that are required.

Alternatively, if you’re only interested in the values that the variables in the workspace hold, you could right click in the workspace and save it. In order to bring all the values back in future, all you need to do is call the saved workspace either using a script, or through the command window. To call something in MATLAB, you need to make sure that it is in your working directory. Then you’d need to type the name of the file in question into the command window, or in a script file that you’d need to run. In the next section, we will talk in depth about importing and exporting data, so don’t worry if this doesn’t make too much sense at this point.