
How to Delete Variables in Matlab?
Let me tell you something about how to clear variables on Matlab.
Why delete variables? You see, if you're working with a lot of data, say big data, you might be using up a considerable amount of your computer's memory, namely the RAM. And when the RAM is almost full, the computer starts using the secondary memory on the hard disk, which could really slow down your data processing. In the worst case scenario, you might even get a memory overflow error due to the software's memory being completely exhausted. So, to avoid these problems, you should periodically clear the workspace on Matlab during your work session.
Now, let me give you a practical example of how the Matlab workspace works.
Say you create three variables and assign them with any numerical values, like so:
>> a=1;
>> b=2;
>> c=3;
Once you create these variables, their names and corresponding assigned values are added to Matlab's workspace.
And every variable in Matlab's workspace occupies a memory address in the computer's memory space.
These variables will remain in the computer's RAM for the current Matlab session, even if you stop using them in your calculations.
Note. Now, when you're dealing with just three variables, the memory space they occupy might seem insignificant. But just imagine if you had millions of variables in the workspace. The memory space occupied unnecessarily would be quite significant. That's when you need to "clean up" your computer's RAM by deleting the unnecessary data.
There are several ways to clear the Matlab workspace.
You can simply type the "clear" command on the command line and hit "enter" to delete all the variables and their corresponding values from the workspace.
>> clear
This command clears out all the variables, freeing up the memory space they occupied. It's like starting a new session.
You can also achieve the same result by using Matlab's menu.
Just click on the "Clear Workspace" button and select "Variables" to free up memory space by deleting all the variables from the current session.
Note. The "All Functions and Variables" menu item removes both variables and functions from memory space. Thus, it frees up a greater amount of memory space.
How to delete a single variable?
If you want to delete only one variable from the Matlab workspace, simply type the "clear" command followed by the name of the variable.
>> clear variableName
This command will only delete the variable that you've indicated, leaving all other variables intact in the workspace.
How to delete a group of variables?
Now, if you want to delete a group of variables from the workspace, you can use special characters such as "?" and "*".
? = any single character
* = zero or more of any characters
For example, if you want to delete all variables that start with "ro" and end with "e", simply type "clear Ro*e".
>> clear ro*e
This command will delete variables such as "Rome", "Rockville", "Rose", "Roseville", and so on.
To delete only variables with a 4-letter name that start with "ro" and end with "e", simply type "clear Ro?e".
>> clear ro?e
This command will delete variables such as "Rome", "Rose", "Rote", and so on.
How to delete global variables?
If you want to delete only global variables from the workspace, just type "clear global".
>> clear global
This command will delete only the global variables from the workspace, leaving all other variables intact.
To delete a specific global variable, simply indicate its name as follows:
>> clear global global_variable_name
How to delete functions?
Finally, to delete only the functions from the Matlab workspace, type "clear functions".
>> clear functions
This command will delete only the functions from the workspace, leaving all other variables intact.
How to delete only variables?
Well, you want to get rid of only the variables from the Matlab workspace, huh? Alright, here's what you do.
Type in the command "clear" followed by the option "variables" like this:
>> clear variables
This command will clear out all the local and global variables in the workspace, but it won't touch any functions you have in there.