Matlab

How to Clear Variables from the Memory in MATLAB- Basic Guide

When we declare any variable in MATLAB, it stores the variable in its workspace window. So these variables occupy the memory of MATLAB also when we declare another variable having the same name as the previously stored variable, MATLAB can use the value of that old variable which can make our code’s results incorrect. To deal with this issue, MATLAB provides us with an option to clear variables from MATLAB’s memory.

If you are not familiar with the way how to clear variables from MATLAB’s memory, follow this guide to learn.

How to Clear Variables from the Memory in MATLAB?

Clearing variables from MATLAB’s memory can be done by utilizing the clear command. This is the widely used method by MATLAB’s users. It is a good approach in MATLAB to use the clear command at the top of any code for effective coding.

Syntax

The clear command has several syntaxes in MATLAB, which are given below:

Clear
clear name1 … nameN
clear global
clear all

 

Here,

The clear command enables us to remove all variables from the workspace window.

The command clear name1 … nameN allows us to delete the specified variable name1, name2…,nameN from the workspace window.

The clear global command is used for deleting all global variables from the workspace window.

The clear all option is used to clear all functions, local and global variables, and Mex files from the workspace window.

Examples

Understand the functionality of the clear command to remove variables from MATLAB’s workspace window using some examples.

Example 1: How to Remove Specified Variables from MATLAB’s Memory Utilizing the clear Command?

This example declares three variables x, y, and z. Then it uses the clear name1,…,nameN command to clear the given variable z from MATLAB’s memory. We use the whos command to find the variable information before and after using the clear command.

x = 7;
y = 'Linuxhint';
z = pi;
whos
clear z;
whos

 

Example 2: How to Remove all Variables from MATLAB’s Memory Utilizing the clear Command?

The given MATLAB code defines some variables and uses the clear all command to clear all given variables from MATLAB’s memory. As the clear command clears all variables no variable will be displayed on the screen depicted from the whos command.

a = 7;
b = 'Linuxhint';
c = pi;
d = 1:10;
e = magic(5);
whos
clear all;
whos

 

Note: In the above screenshot, the whos command after the clear all command will not display any variable since all variables are cleared.

Conclusion

MATLAB facilitates us with a way to clear variables from memory. This can be performed utilizing the clear command. The clear command allows us to clear one, more, or all variables stored in the workspace window to free MATLAB’s memory resources. This method is widely used and adopted by MATLAB users. This tutorial has explored the usage of clear command to clear variables from MALAB’s memory.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.