Graphics Programs Reference
In-Depth Information
error('No current figure.')
end
flag = ~ishold;
if flag
hold on
end
disp('Click on the point where you want to plot an X.')
[x, y] = ginput(1);
plot(x, y, 'xk')
if flag
hold off
end
First the M-file checks to see if there is a current figure window. If so, it
proceeds to set the variable flag to 1 if hold off is in effect and 0 if hold
on is in effect. The reason for this is that we need hold on in effect to plot
an “X” without erasing the figure, but afterward we want to restore the figure
window to whichever state it was in before the M-file was executed. The M-file
then displays a message telling the user what to do, gets the coordinates of the
point selected with ginput(1) , and plots a black “X” at those coordinates.
The argument 1 to ginput means to get the coordinates of a single point;
using ginput withno input argument would collect coordinates of several
points, stopping only when the user presses the ENTER key.
In the next chapter we describe how to create a GUI (Graphical User Inter-
face) within MATLAB to allow more sophisticated user interaction.
Evaluation
The commands eval and feval allow you to run a command that is stored
in a string as if you had typed the string on the command line. If the entire
command you want to run is contained in a string str , then you can exe-
cute it with eval(str) . For example, typing eval('cos(1)') will produce
the same result as typing cos(1) . Generally eval is used in an M-file that
uses variables to form a string containing a command; see the online help for
examples.
You can use feval on a function handle or on a string containing the name
of a function you want to execute. For example, typing feval('atan2', 1,
0) or feval(@atan2, 1, 0) is equivalent to typing atan2(1, 0) . Often
feval is used to allow the user of an M-file to input the name of a function
to use in a computation. The following M-file iterate.m takes the name of a
Search WWH ::




Custom Search