MATLAB is a powerful mathematical software package that is used for designing and testing algorithms. While working with MATLAB, we may define a lot of variables and data in the workspace. After defining multiple variables, it can be difficult for us to track our work and can also slow down our MATLAB session. So, we must be looking for a way to clear MATLAB. This article covers various ways of cleaning the MATLAB workspace including the command window and variables.
How to Clear in MATLAB
For demonstration, first, we will define some variables to clear them later in MALAB using different commands. Here we have defined two variables a and b:
b = 4;
Now as we have defined two variables, we will move towards the method of removing them.
How to Clear All Variables
In MATLAB we can clear variables from the workspace using the clear command. For example, the following command will clear all variables from the workspace:
How to Clear Specific Variables
If we only want to clear specific variables from the workspace, we can use the clear command with the variable names as arguments. The below command will clear the variables b only:
Only variable a is now present in the MATLAB workspace.
How to Clear Figures in MATLAB
If we have created any figures in MATLAB, we can clear them using the close command. The below MATLAB commands close all figures and plots:
How to Clear the Command History
In MATLAB all the commands we type in the command window will appear in the history of the command window. We can clear the command history using the clc command. Below given clc command will clear the command window history:
After typing clc all the commands from the command window will be clear.
How to Clear the Workspace
The workspace is a collection of all the variables, figures, and other data that is currently in use in MATLAB sessions. We can clear the workspace using the clear command. Following commands clear MATLAB workspace:
After typing the clear command all of the workspace variables will be removed.
Conclusion
Clearing your MATLAB workspace can be a helpful way to improve performance and keep your work organized. You can clear all variables, specific variables, figures, the command history, or the entire workspace. Read more about clearing different parameters in MATLAB in this article.