
9 Managing Application-Defined Data
9-8
The GUI behavior is as follows:
• When a user moves the slider, the text box displays the slider’s current value.
• When a user types a value into the text box, the slider updates to this value.
• If a user enters a value in the text box that is out of range for the slider —
that is, a value that is not between 0 an 1 — the application returns a
message in the edit text box indicating how many times the user has entered
an erroneous value.
The commands given in the following steps initialize the error counter and
implement the interchange between the slider and the edit text box.
1 Define the error counter in the opening function. The GUI records the
number of times a user enters an erroneous value in the text box and stores
this number in a field of the
handles structure. Start by defining this field,
called
number_errors, in the opening function as follows:
handles.number_errors = 0;
Type this statement before the following line, which GUIDE automatically
inserts into the opening function.
guidata(hObject,handles); % Save the updated handles structure
The guidata command saves the handles structure so that it can be
retrieved in the GUI’s callbacks.
2 Set the edit text value from the slider Callback callback. The following
command in the slider
Callback updates the value displayed in the edit text
when a user moves the slider and releases the mouse button.
set(handles.edit1,'String',...
num2str(get(handles.slider1,'Value')));
The code combines three commands:
- The
get command obtains the current value of the slider.
- The
num2str command converts the value to a string.
- The
set command sets the String property of the edit text to the updated
value.
Kommentare zu diesen Handbüchern