To clear all variables in MATLAB, you can use the commands clear
or clearvars
for the current workspace, and clear global
or clearvars -global
for global variables.
Understanding MATLAB Workspace
The MATLAB workspace is the memory space where variables are stored during a MATLAB session. When you create a variable (like x = 5;
), it resides in the current workspace. As your programs run, the workspace can accumulate many variables.
Clearing Variables in the Current Workspace
The most common task is clearing variables from the current workspace. This is useful for starting fresh between different code snippets or analyses, ensuring that results aren't affected by variables left over from previous operations.
According to the reference: To clear all variables from the current workspace, use clear
or clearvars
.
Both commands achieve the same result when used without arguments to clear everything.
clear
: A fundamental command in MATLAB for removing variables, functions, and MEX-files from memory.clearvars
: A more modern command, specifically designed for clearing variables. It offers more options for selectively clearing variables based on criteria like type or pattern, but when used alone, it clears all.
Examples
Here's how you use these commands:
% Create some variables
a = 10;
b = 'hello';
c = [1 2 3];
% View variables in the workspace (optional)
whos
% Clear all variables using clear
clear
% View workspace again (should be empty)
whos
Alternatively, using clearvars
:
% Create some variables
x = 99;
y = magic(3);
% View variables
whos
% Clear all variables using clearvars
clearvars
% View workspace again
whos
Both clear
and clearvars
without any arguments effectively clear all variables from your current workspace.
Clearing Global Variables
MATLAB also supports global variables, which are accessible across different functions and scripts. Clearing these requires a specific command to avoid unintentionally affecting other parts of your application that might rely on them.
The reference states: To clear all global variables, use clear global
or clearvars –global
.
clear global
: Clears all variables that have been declared as global.clearvars -global
: Aclearvars
specific option to clear only global variables.
Examples
Let's see how this works:
% Declare and assign a global variable
global myGlobalVar;
myGlobalVar = 123;
% Declare a regular variable
localvar = 456;
% View variables (myGlobalVar might not show with 'whos' directly
% unless within a function that declares it global, or you use 'whos global')
whos global % Shows global variables
% Clear only global variables
clear global
% View variables again (myGlobalVar should be gone, localvar remains if in current scope)
whos global
whos % localvar is still here if in the same scope
Using clearvars -global
:
% Declare and assign a global variable
global anotherGlobal;
anotherGlobal = 'data';
% Clear only global variables using clearvars
clearvars -global
% View variables again
whos global % anotherGlobal should be gone
Summary Table
Here's a quick summary of the commands for clearing variables:
Command | What it Clears |
---|---|
clear |
All variables in the current workspace |
clearvars |
All variables in the current workspace |
clear global |
All global variables |
clearvars -global |
All global variables |
Best Practices
- Use
clear
orclearvars
at the beginning of a script or function if you want to ensure it runs in a clean environment, free from previous variables. - Be cautious when using
clear
orclearvars
in larger projects, as you might inadvertently remove variables needed later in the code. - Use
clear global
orclearvars -global
with care, as global variables are often used to share data across multiple parts of a program. Clearing them might disrupt functionality elsewhere. - To clear specific variables, you can list their names after
clear
orclearvars
(e.g.,clear var1 var2
).
By using these commands, you can manage the MATLAB workspace effectively, keeping it tidy and preventing unwanted variable interference.