Ever been in the situation where you accidently used a variable named something else in MATLAB and it caused you a problem? Ever done it with a variable named clear? It can be devilish to get out of the mess. Here are some ways.
Contents
First let's set up the situation.
clear = 'mud'
zerosAndOnes = [true true false]clear =
mud
zerosAndOnes =
1 1 0
Now we realize we don't need zerosAndOnes. Here's what happens if I try to clear that variable.
Attempt #1
clear zerosAndOnes ??? Error: "clear" was previously used as a variable, conflicting with its use here as the name of a function or command. See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax" for details.
Attempt #2
clear('zerosAndOnes')
??? Error: "clear" was previously used as a variable,
conflicting with its use here as the name of a function or command.
See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax"
for details.Attempt #3
clear(zerosAndOnes) ans = mu
Attempt #4
clear variables ??? Error: "clear" was previously used as a variable, conflicting with its use here as the name of a function or command. See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax" for details.
What to Try Now?
Here are some thoughts on what to try next (and behold, they each work).
- Open the Workspace browser, select the variable named zerosAndOnes and delete it with the icon containing the red X, or right click on the variable and select "Delete"
- Use the function builtin to call the built-in version of clear.
- quit can help you out, if you are willing and able to start your MATLAB session over.
who
Your variables are: clear myThoughts zerosAndOnes
builtin('clear','zerosAndOnes') who
Your variables are: clear myThoughts
Of course, what I should do here is clear the variable named clear itself now and name it something else.
builtin('clear','clear') myThoughts = 'mud' who
myThoughts = mud Your variables are: myThoughts
More Ways?
What have you used to get out of such a snarl in MATLAB? Did you know about the function builtin (and that it can't be used with MATLAB code defined in text files, but truly only for ones that are compiled into the MATLAB executable)? Tell us your stories here.
Get
the MATLAB code
Published with MATLAB® 7.11



Out of curiousity I was wondering if there was a way out if one had assigned to variables named ‘clear’ and ‘builtin’. I don’t use the GUI, and calling quit seems like admitting defeat.
The solution I came up with was to create an m-file called helpme.m with the contents below.
Then I could do:
clear = helpme(); clear('clear');Iain-
You are right – without the gui, function handles can help you get out of the mess, esp. inside from inside a function as you have done.
Here’s another way, without needing to create a file or function.
cl = str2func('clear') cl()Of course, cl clears itself as well as the variable clear :-)
–Loren
Yes, I know about builtin not working with non-compiled functions – it is particularly annoying when you want to define a method of the same name as the function from which you’d like to call the “builtin”.
Of course, a better solution would be for the smart folks who write MATLAB to add “clear” to the list of reserved words you can’t assign to, right? You know anybody in a position to do that, Loren? :-)
All this stuff just because MatLab do not have separate namespaces (like packages in java, etc.) with smart conflict resolving policy. A little warning like «you attempting to override a built-in method» can be enough.
Jonathan-
It might – or we might find another solution. But it is something we want users to not have to be so aware of.
–Loren
feval(‘clear’,'a’)