Graphics Programs Reference
In-Depth Information
Forgetting to clear or reset variables.
CAUSE:
SOLUTION: Clear or initialize variables before using them, especially in a long ses-
sion.
See Variables and Assignments in Chapter 2.
Conflicting definitions.
CAUSE:
SOLUTION: Do not use the same name for two different functions or variables, and
in particular, try not to overwrite the names of any of MATLAB's built-in
functions.
You can accidentally mask one of MATLAB's built-in M-files either with your
own M-file of the same name or with a variable (including, perhaps, an inline
function). When unexpected output occurs and you think this might be the
cause,ithelpstouse which tofindoutwhatM-fileisactuallybeingreferenced.
Here is perhaps an extreme example.
EXAMPLE:
>> plot = gcf;
>> x = -2:0.1:2;
>> plot(x, x.ˆ2)
Warning: Subscript indices must be integer values.
??? Index into matrix is negative or zero. See release
notes on changes to logical indices.
What's wrong, of course, is that plot has been masked by a variable with the
same name. You could detect this with
>> which plot
plot is a variable.
If you type clear plot and execute the plot command again, the prob-
lem will go away and you'll get a picture of the desired parabola. A more
subtle example could occur if you did this on purpose, not thinking you would
use plot , and then called some other graphics script M-file that uses it
indirectly.
CAUSE: Not keeping track of ans .
SOLUTION: Assign variable names to any output that you intend to use.
If you decide at some point in a session that you wish to refer to prior output
that was unnamed, then give the output a name, and execute the command
Search WWH ::




Custom Search