Create three matrices using the following code:
A = 1; B = [1, 2, 3]; C = [4, 5; 7, 8]; whos;
A = 1
B = 1 2 3
C = 4 5 7 8
Name Size Bytes Class Attributes A 1x1 8 double B 1x3 24 double C 2x2 32 double
clearvars deletes all variables in the current workspace.
clearvars(var1,var2,…,varN) deletes the variables specified by the strings
% Delete the variables A and B clearvars('A','B'); whos;
Name Size Bytes Class Attributes C 2x2 32 double
% Delete all variables clearvars;
clear clears items existing in the workspace, including variables.
clear deletes all variables in the current workspace.
clear(item1,item2,…,itemN) clears the workspace items specified by the strings
% Create two variables D = 'Hello World!'; E = 9;
D = Hello World!
E = 9
% Delete the variable D clear('D');
% Delete all variables clear;
Note: On UNIX systems,
clear do not affect the amount of memory allocated to the MATLAB process.
I recommend the following books to learn more on the MATLAB environment.
This book was the winner of a 2017 Textbook Excellence Award. The first chapter explains the windows of the MATLAB environment, among other introductory topics like operators, random numbers, and trigonometry.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The second chapter explains the windows of the MATLAB environment, how to name variables, and how to save a series of commands in an .M file, among other topics.
I also recommend the following pages in the MATLAB help documentation, and the other tutorials in this series.
doc clearvars; doc clear;
- Getting Help in MATLAB
- Valid Identifiers in MATLAB
- Reserved Words in MATLAB
- Comments in MATLAB
- Multiline Statements in MATLAB
- How to Execute a Command Automatically When MATLAB Starts
- How to Execute a Command Automatically When MATLAB Quits
- How to Save the Workspace Variables to a .MAT File in MATLAB
- How to Load the Variables in a .MAT File into the Workspace in MATLAB
- How to Delete a Variable in MATLAB
I hope you have learned how to delete a variable in MATLAB. The source code developed in this tutorial is available at this page.