If your MATLAB session has accumulated old variables, large arrays, or results from previous calculations, clearing the workspace can help you start fresh. The quickest way to clear the current workspace in MATLAB is to enter clear in the Command Window. You can also use clearvars to remove variables, delete only selected items, or clear variables through the Workspace panel.
The right method depends on what you want to remove. Clearing workspace variables is different from clearing the Command Window, closing figures, or resetting functions. This guide explains how to clear the MATLAB workspace safely, how to keep specific variables, and which commands to use for common cleanup tasks.
What Does the MATLAB Workspace Contain?
The MATLAB workspace holds variables created or loaded during a session. These can include numbers, text, tables, structures, cell arrays, and large data sets. The Workspace panel lists variables available in the active workspace, along with details such as their values, sizes, and types.
MATLAB can have more than one workspace. Commands entered directly in the Command Window and scripts typically use the base workspace. Each function has its own local workspace, which is separate from the base workspace. As a result, clearing the base workspace does not necessarily remove variables inside a function that is currently running.
Clearing a workspace removes variables from memory; it does not delete your MATLAB scripts, functions, or data files saved on your computer. However, unsaved variable values are lost, so save anything you need before clearing.
Clear the Entire Workspace with clear
To remove variables from the current workspace, type this command in the MATLAB Command Window and press Enter:
clear
In the base workspace, this removes the variables currently available there. The Workspace panel should then show that those variables are gone. You can use the same command in a script when you want it to remove variables from the workspace in which the script runs.
MATLAB also accepts clear followed by variable names. This lets you remove specific variables without clearing everything:
clear x results tempData
Use this approach when you want to keep useful variables but discard temporary calculations. If a listed name is not present, MATLAB generally continues without clearing anything for that name.
Use clearvars to Remove Workspace Variables
clearvars is a workspace-focused alternative that removes variables from the current workspace. With no extra options, it clears all variables there:
clearvars
It is especially useful when you want to control exactly which variables remain. For instance, use -except to clear everything except the names you specify:
clearvars -except data model
After this command, data and model remain in the current workspace, while other variables are removed. This can be handy when you want to rerun an analysis but keep a data set or a trained model in memory.
You can also remove variables whose names match a pattern with the -regexp option. For example, the following command clears variables whose names begin with “temp”:
clearvars -regexp ^temp
Regular-expression patterns can be useful in workspaces with many similarly named variables. Check the pattern before running the command, particularly if you have important variables that could match it.
Clear Variables Using the Workspace Panel
If you prefer using MATLAB’s desktop interface, clear variables from the Workspace panel. The exact layout can vary by MATLAB release and desktop arrangement, but the general process is similar:
- Find the Workspace panel in the MATLAB desktop. If it is hidden, open it from the desktop layout or panel options.
- Select one or more variables you want to remove. Use the usual selection keys for your operating system to select multiple items.
- Press the Delete key or use the panel’s context menu to choose the clear or delete option.
- Confirm that the selected variables no longer appear in the panel.
To clear every variable, you can select all entries in the panel and remove them, or use clear or clearvars in the Command Window. The command is usually faster, while the panel makes it easier to inspect variable names before removing them.
Understand clear, clc, and close all
Several MATLAB commands are often used together at the beginning of a script, but they do different jobs. Knowing the distinction helps prevent confusion when the Workspace panel still contains variables after you have cleaned up the screen.
-
clearorclearvarsremoves variables from the current workspace. -
clcclears the text displayed in the Command Window. It does not remove workspace variables. -
close allcloses open figure windows. It does not clear variables or erase Command Window text.
For example, this common setup clears variables, clears the Command Window display, and closes figure windows:
clearvars
clc
close all
Running these commands together can make a new analysis easier to follow, but remember that only the first command clears workspace variables. You do not need to use all three if your goal is simply to remove variables.
Clear Specific Variables Without Losing Everything
Clearing the whole workspace may be unnecessary when only a few variables are temporary. To remove selected variables, provide their names to clear or clearvars:
clearvars temporaryImage oldResults
This is a useful habit in longer sessions, where you may want to keep input data, settings, or intermediate results for comparison. Use who to see variable names in the current workspace, or whos to see names along with information such as size and memory usage.
For example, if whos shows that a large array is consuming memory, you can remove just that array:
clear largeArray
Check that the variable name is correct before clearing it. Once an unsaved variable has been removed, MATLAB cannot restore its value unless you recreate it or load it from a saved file.
Clear the Workspace Before Running a Script
You can put a clearing command at the top of a script so it begins with a predictable workspace. For example:
clearvars
clc
% Analysis code starts here
Use this only when the script is meant to run independently. If the script expects variables that you created earlier in the Command Window, clearing those variables will cause the script to fail or produce different results. A script shares the base workspace, so its clearing command can affect variables outside the script as well.
Functions behave differently. A function uses its own local workspace, and its local variables are normally discarded when the function finishes. This separation makes functions less dependent on unrelated variables left in the base workspace. If you need to clear or inspect data inside a function, do so within the function’s own code and consider whether a cleanup command is actually necessary.
What About Global and Persistent Variables?
Global variables are shared between workspaces only when code declares them with the global keyword. If you use globals, be deliberate about clearing them. You can remove global variables with:
clear global
Persistent variables are stored between calls to a function rather than being recreated each time. This can make a function retain state even after its local variables would normally disappear. If you need to reset persistent state, MATLAB provides commands for clearing functions, but use them carefully because they affect function state rather than just ordinary workspace variables.
Should You Use clear all?
You may see clear all in older MATLAB examples. It is broader than clearing ordinary variables: it can also remove functions from memory and reset persistent function data. That extra work may slow down later execution as MATLAB loads or prepares code again.
For routine workspace cleanup, prefer clearvars or clear. Use function-clearing commands only when you specifically need to reset function state, for example while developing code that uses persistent variables. Avoid using clear all as an automatic first step unless you understand the additional effects and need them.
Common Problems When Clearing the Workspace
-
The Workspace panel is empty, but the Command Window still shows text: Run
clcto clear the displayed text. Clearing variables does not clear the Command Window. -
The Command Window is blank, but variables remain:
clconly clears the display. Runclearvarsorclearto remove variables. -
Figures are still open: Run
close all. Figures are separate from workspace variables. - A variable remains after a script runs: Scripts use the workspace in which they run. Check whether the script contains a clearing command and whether it is clearing the intended workspace.
- A function seems to retain a value: Check whether the function uses a persistent or global variable. Ordinary workspace cleanup may not reset state stored by the function.
Verify That the Workspace Is Clear
After clearing variables, run who to list names remaining in the current workspace. If nothing is listed, there are no ordinary named variables there. Run whos when you also want information about variable sizes and types.
Keep in mind that these commands report the current workspace. If you run them inside a function, they show that function’s local variables, not necessarily everything in the base workspace. To inspect the base workspace, run the commands directly in the Command Window.
Conclusion
The simplest way to clear the MATLAB workspace is to run clear or clearvars in the Command Window. Use clearvars -except when you want to remove most variables but keep selected ones, and use the Workspace panel when you prefer to choose variables visually. Remember that clc clears Command Window text and close all closes figures; neither command clears workspace variables. Choose the narrowest command that fits your task, and save any values you may need before removing them.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.