Graphics Programs Reference
In-Depth Information
plotting the wrong kind of object, and
using uppercase instead of lowercase letters in MATLAB commands, or
misspelling commands.
Debugging Techniques
Now that we have discussed the most common mistakes, it's time to discuss
how to debug your M-files, and how to locate and fix those pesky problems
that don't fit into the neat categories above.
If one of your M-files is not working the way you expected, perhaps the
easiest thing you can do to debug it is to insert the command keyboard some-
where in the middle. This temporarily suspends (but does not stop) execution
and returns command to the keyboard, where you are given a special prompt
witha K in it. You can execute whatever commands you want at this point
(for instance, to examine some of the variables). To return to execution of the
M-file, type return or dbcont , short for “debug continue.”
A more systematic way to debug M-files is to use the MATLAB M-file
debugger to insert “breakpoints” in the file. Usually you would do this with
the Breakpoints menu or with the “Set/clear breakpoint” icon at the top of
the Editor/Debugger window, but you can also do this from the command line
withthe command dbstop . Once a breakpoint is inserted in the M-file, you
will see a little red dot next to the appropriate line in the Editor/Debugger. (An
example is illustrated in Figure 11-8 below.) Then when you call the M-file,
execution will stop at the breakpoint, and just as in the case of keyboard ,
control will return to the Command Window, where you will be given a special
prompt witha K in it. Again, when you are ready to resume execution of the
M-file, type dbcont . When you are done with the debugging process, dbclear
“clears” the breakpoint from the M-file.
Let's illustrate these techniques with a real example. Suppose you want
to construct a function M-file that takes as input two expressions f and g
(given either as symbolic expressions or as strings) and two numbers a and b ,
plots the functions f and g between x = a and x = b , and shades the region in
between them. As a first try, you might start with the nine-line function M-file
shadecurves.m given as follows:
function shadecurves(f, g, a, b)
%SHADECURVES Draws the region between two curves
% SHADECURVES(f, g, a, b) takes strings or expressions f
% and g, interprets them as functions, plots them between
%x=aandx=b,andshades the region in between.
Search WWH ::




Custom Search