I recently wrote an article on Beginner Woes, and I thought I would add some more gotcha topics today.
Contents
Clearing Everything
I've seen people in the middle of a session type clear all and then try to "continue" debugging. Even more insidious is when clear all is buried in some code that the program being debugged calls. The problem is that clear all clears more than just the workspace. To quote from the reference page:
Removes all variables, functions, and MEX-files from memory, leaving the workspace empty. Using clear all removes debugging breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. When issued from the Command Window prompt, also removes the Java packages import list.
To clear just variables, use clear variables instead. clear all is a very heavyweight tool for some chores.
Working with mlock'ed Files
mlock allows you to depend on M-file and MEX-files not being cleared from memory. This, in turn, allows you to be sure that persistent variables stay in memory. However, when you are editing a file that uses mlock, and you are unaware that it is locked, you may be mystified why MATLAB is not recognizing your file changes. In this case, you must use munlock, clear the file, and then run the desired code again. The function mislocked can tell you that status of the file in question.
Variable Names Worth Avoiding
Many people have commented on MATLAB's flat namespace. This is sometimes a positive and sometimes a negative. If you name a variable the same name as a MATLAB function, the variable shadows the function. We have seen several posts to the MATLAB newsgroup questioning why the user gets an error trying to put a title on a plot, for example. The usual reason is that the user has a variable named "title" that interferes with the function. Here are two of the possible error messages. (They are hard to reproduce in the published M-file.)
emsg1 = sprintf(['Error: "cd" was previously used as a variable,\n', ... 'conflicting with its use here as the name of a function or command.\n ', ... 'See MATLAB Programming, "Determining Which Function Is Called" for details.']) emsg2 = 'Index exceeds matrix dimensions.'
emsg1 = Error: "cd" was previously used as a variable, conflicting with its use here as the name of a function or command. See MATLAB Programming, "Determining Which Function Is Called" for details. emsg2 = Index exceeds matrix dimensions.
Typical "CULPRITS" - variable names that often cause problems:
- cd
- length
- size
- title
Clearing Axes
Recently there was a post on the MATLAB newsgroup about a "bug" in clearing the axes after a comet plot. When you type
comet(x,y)
you see this plot
t = 0:pi/40:2*pi; x = -sin(4*t); y = cos(2*t); cometplot
The user reasonably expected the cla command to clear the axes. cla left the full line in the axes, as in the previous plot.
However, comet does not use the default mode for erasing lines so it can leave a trail. (For more on erasemodes, look here.) So here's what you see on the screen after issuing cla.
cometplotcla
Because of the non-default erasemode, issuing cla reset is in order instead of the default cla. Here's what you see then.
cometplotreset
Know of Other Gotchas?
Do you happen to have other gotchas that are worth sharing? Post them here please.
Get
the MATLAB code
Published with MATLAB® 7.4

A common reason for problems is to use i or j as a loop index, overwriting the imaginary unit.
Markus
Hi Loren,
When I first started solving problems with Matlab, one “woe” or “gotcha” I frequently had was knowing when to use {} versus () when dealing with cell arrays. The fact that index statements and assignment statements can have different syntax is counter-intuitive. It took some time before I could confidently select one without trial and error.
Hope this helps. BTW, love the blogs here (all of them).
Rob
About mlock and persistent. I was trying to protect a global variable from resetting by using mlock, but this doesn’t work. And you can’t define a variable as both global and persistent. Is there a way around this. (I’ll also ask the newsgroup.)
a response to myself: A search in the newsgroup archive revealed that I had already asked this question there, and Loren had answered it! I completely forgot about it, the human brain is so unreliable… Thanks Loren
btw, the answer was that mlock protects one file, while the same global variable can be defined in many files. you can see the post in the newsgroup archive:
http://newsreader.mathworks.com/WebX/.ef38ad4?50@382.HITabyR53yv@
Hello Loren,
a very common mistake is using i or j as an index in a for loop. After that calculations with a complex nuumbers give often an unexpecting result.
The reason is the same like in your examplex, but you will not receive an error message, which makes this a little bit more dangerous.
Michael
One Gotcha that got me recently was the use of ‘javaaddpath’.
The code was something as follows
function foo()
javaaddpath(’\\share\cat_code\java’);
global cat;
if (cat == ’siamese’)
fprintf(1, ‘Meow\n’);
end
end
The problem that would occur is that cat did not seemed to be picked up as a global and hence the condition was never triggered.
I eventually traced it down to the javaaddpath command, which calls javaclasspath which makes a call to “clear(’java’)”.
Looking into the documentation :
clear java The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path
…. oh… so I just did clear all… fantastic! Just what I expected to happen when I add an entry to the Java path.
I just discovered this too. It is undocumented and ridiculous. But how do you add something to the classpath without clearing everything?
Hi Loren,
Is it useful to clear local variables inside function when they are huge?
I see the point of freeing memory when working in the base workspace but MATLAB is freeing local variables when exiting a function, right?
So what’s the need of clearing them at the end of the function. Is there any speed gain in execution?
Keep up the good work.
Christophe
Christophe-
The function workspace will be cleaned up when you exit the function so there is no need to manually clear the variables. If you have a large variable that has changed in the middle of the function and you want to do a lot more before returning from the function, clearing the large variable *may* be beneficial.
–Loren