Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the variable "clear" in MATLAB

Tags:

matlab

Let's say you are some new programmer and you do something like...

%...la da da
%...programming away
if such && such
    clear = 1;
else 
    clear = 0;
end 

or in some other way, you assign the variable clear a value.

Is there some way to "clear" clear?

clearvars doesn't work. Clicking the workspace variable and manually clicking delete does work, but I think it's cheating.

like image 571
Frederick Avatar asked Aug 21 '13 14:08

Frederick


People also ask

How do you not clear a variable in MATLAB?

clearvars -except keepVariables removes all variables, except for those specified by keepVariables . Use this syntax to keep specific variables and remove all others. clearvars variables -except keepVariables removes the variables specified by variables , and does not remove the variables specified by keepVariables .

What is clear command in MATLAB?

example. clc clears all the text from the Command Window, resulting in a clear screen. After running clc , you cannot use the scroll bar in the Command Window to see previously displayed text. You can, however, use the up-arrow key ↑ in the Command Window to recall statements from the command history.

Which command is used to remove variables from memory?

The drop command is used to remove variables or observations from the dataset in memory.

What is the difference between clear and clear all in MATLAB?

clear FUNCTIONS removes all compiled MATLAB and MEX-functions. clear ALL removes all variables, globals, functions and MEX links. clear ALL at the command prompt also clears the base import list.


3 Answers

This will do it:

builtin('clear','clear')

Note: Keep in mind to avoid such operations to keep code clarity. Only do overwrite when it is the exact action you want to take place. Otherwise it may cause future bugs if you forgot (or if another person uses your code and didn't realize it) that you have the clear (or any other) function overwritten. You could easily name this variable as doClear for example.

like image 107
Buck Thorn Avatar answered Sep 18 '22 21:09

Buck Thorn


Any name, even builtin and feval can be overriden. In such case, you can use function handles instead to force MALTAB into interpreting a statement as a function call:

clear = str2func('clear');
clear('clear')

Obviously, str2func can also be overrriden! :) however, there exists a similar solution (inspired by Loren's article), which is creating a separate m-file that does the same thing:

function clearclear()
    assignin('caller', 'clear', @clear);

Calling this function in the main workspace should allow you to do clear('clear') safely.

The second solution takes advantage of the fact that the m-file doesn't "see" the variable clear in the main workspace, and therefore can access the actual handle of the clear function properly.

like image 35
Eitan T Avatar answered Sep 18 '22 21:09

Eitan T


A non intuitive way is

clear = rand(1000,500,700);
pack

This produces the following warning:

Warning: Variable 'clear' cannot be saved to a MAT-file whose version is older than 7.3. To save this variable, use the -v7.3 switch. Skipping...

It also suffers from the same issue that you can assign pack to be a variable.

like image 38
StrongBad Avatar answered Sep 19 '22 21:09

StrongBad