I recently wrote an article on Beginner Woes, and I thought I would add some more gotcha topics today.
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.
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.
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:
Recently there was a post on the MATLAB newsgroup about a "bug" in clearing the axes after a comet plot. When you type
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. So here's what you see on the screen after issuing cla.
Because of the non-default erasemode, issuing cla reset is in order instead of the default cla. Here's what you see then.
Do you happen to have other gotchas that are worth sharing? Post them here please.