Graphics Programs Reference
In-Depth Information
commandpromptorwithinanM-file,withthecommand
warning off
.There
are several other options for how MATLAB should handle warning messages;
type
help warning
for details.
In Chapter 4 we used
disp
to display the output of a command without
printing the “
ans =
” line. You can also use
disp
to display informational
messages on the screen while an M-file is running, or to combine numerical
output with a message on the same line. For example, the commands
x=2+2;disp(['The answer is ' num2str(x) '.'])
will set
x
equal to
4
and then print
The answer is 4.
MATLAB also has several commands to solicit input from the user run-
ning an M-file. At the end of Chapter 3 we discussed three of them:
pause
,
keyboard
, and
input
. Briefly,
pause
simply pauses execution of an M-file
until the user hits a key, while
keyboard
bothpauses and gives the user a
prompt to use like the regular command line. Typing
return
continues ex-
ecuting the M-file. Lastly,
input
displays a message and allows the user to
enter input for the program on a single line. For example, in a program that
makes successive approximations to an answer until some accuracy goal is
met, you could add the following lines to be executed after a large number of
steps have been taken:
answer = input(['Algorithm is converging slowly; ', ...
'continue (yes/no)? '], 's');
if ~isequal(answer, 'yes')
return
end
Here the second argument
's'
to
input
directs MATLAB not to evaluate
the answer typed by the user, just to assign it as a character string to the
variable
answer
.Weuse
isequal
to compare the answer to the string
'yes'
because
==
can only be used to compare arrays (in this case strings) of the
same length. In this case we decided that if the user types anything but the
full word
yes
, the M-file should terminate. Other approaches would be to
only compare the first letter
answer(1)
to
'y'
, to stop only if the answer is
'no'
, etc.
If a figure window is open, you can use
ginput
to get the coordinates of a
point that the user selects with the mouse. As an example, the following M-file
prints an “X” where the user clicks:
function xmarksthespot
if isempty(get(0, 'CurrentFigure'))
Search WWH ::
Custom Search