Matlab

How to Clear MATLAB

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:

a = 9;
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:

clear

 

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:

clear b

 

Only variable a is now present in the MATLAB workspace.

whos

 

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:

close all

 

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:

clc

 

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:

clear

 

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.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.