Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

A Clear Conundrum

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)?




Published with MATLAB® 7.11


  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.