MATLAB Creating Graphical User Interfaces

(Barry) #1

9 Examples of GUIDE UIs


UI was saved. For more information on setting tags, see “Identify the Axes” on page 9- 23
in the previous example.

The handles.Strings data is used in the spinstopbutton_Callback function,
which includes the following code for changing the label of the button:

str = get(hObject,'String');
state = find(strcmp(str,handles.Strings));
set(hObject,'String',handles.Strings{3-state});

The find function returns the index of the string that matches the button's current
label. The call to set switches the label to the alternative string. If state is 1 , 3-state
sets it to 2. If state is 2 , it sets it to 1.

Interrupt the Spin Callback

If you click the Spin/Stop button when its label is Stop, its callback is looping through
code that updates the display by advancing the rotation of the surface objects. The
spinstopbutton_Callback contains code that listens to such events, but it does not
use the events structure to accomplish this. Instead, it uses this piece of code to exit the
display loop:

if find(strcmp(get(hObject,'String'),handles.Strings)) == 1
handles.azimuth = az;
guidata(hObject,handles);
break
end

Entering this block of code while spinning the view exits the while loop to stop the
animation. First, however, it saves the current azimuth of rotation for initializing the
next spin. (The handles structure can store any variable, not just handles.) If you click
the (now) Spin button, the animation resumes at the place where it halted, using the
cached azimuth value.

When you click Quit, the UI destroys the figure, exiting immediately. To avoid errors due
to quitting while the animation is running, the while loop must know whether the axes
object still exists:

while ishandle(handles.axes1)
% plotting code
...
end
You can write the spinstopbutton_Callback function without a while loop, which
avoids you having to test that the figure still exists. You can, for example, create a timer
Free download pdf