nahomyaja
nahomyaja

Reputation: 202

How to get back clear command if we assign a value to it (MATLAB)?

In MATLAB, built-in functions can be assigned a variable value like plot = 5. From then on the function plot() will not be available. plot(x), for a variable x, will give a compilation error. To get back the function we just have to delete the variable plot by using clear plot.

clear is the command usually used to get back the built-in function.

My question is if we assign a value (scalar or matrix) to the function clear, how do we get back the function clear?

clear clear won't obviously work. I couldn't think of a way to get the function back, other than closing and restarting MATLAB.

Upvotes: 2

Views: 142

Answers (2)

matlabgui
matlabgui

Reputation: 5672

As already mentioned its very bad practice to shadow matlab functions like clear - however in this case you can use builtin to clear your clear variable:

>> clear = 1
>> whos
  Name       Size            Bytes  Class     Attributes

  clear      1x1                 8  double              

>> builtin clear
>> whos
>> 

To only clear clear use:

builtin clear clear

Upvotes: 7

Ander Biguri
Ander Biguri

Reputation: 35525

No, clear is the function to clear, delete, a variable. if you clear the variable that was "shadowing" your function, then the function can be found again. if you "shadow" the clear function, then you have no way of clearing anything anymore!

It is very bad practice to name things plot clear surf conv or any other MATLAB function, because of shadowing problems, and you clearly discovered why.

Never shadow a variable in MATLAB!


@Jucobs gives a very nice hint in the comments. Use exist. It will return a different value if the thing is a variable (1) or a function (2,5)

Upvotes: 1

Related Questions