Specifications
Managing GUI Data with the Handles Structure
4-11
Defining the Data Fields During Initialization
The following excerpt from the GUI setup code show two additional fields
defined in the
handles structure – errorString and numberOfErrors:
•
guihandles creates the structure and adds the handles of the slider and edit
text using the
Tag property to name the fields (edit1 and slider1).
•
guidata saves the handles structure. This function can return the handles
structure as well.
fig = openfig(mfilename,'reuse');
handles = guihandles(fig);
% Create structure for the first time
handles.errorString = 'Total number of errors: ';
handles.numberOfErrors = 0;
guidata(fig,handles); %
Save the updated structure
Setting the Edit Text Value from the Slider Callback
Use the handles structure to obtain the handles of the edit text and the slider
and then set the edit text
String to the slider Value.
set(handles.edit1,'String',...
num2str(get(handles.slider1,'Value')));
Note GUIDE-generated subfunctions take the handles structure as an
argument. This eliminates the need to call
guidata from within a subfunction
to return the structure. However, if you make any changes to the
handles
structure, you must use guidata to save these changes.
Setting the Slider Value from the Edit Text Callback
The edit text callback routine sets the slider’s value to the number the user
types in, after checking to see if it is a single numeric value within the range of
values allowed by the slider. If the value is out of range, then the error count is
incremented and the error string and the error count are displayed.
val = str2double(get(handles.edit1,'String'));
if isnumeric(val) & length(val)==1 & ...
val >= get(handles.slider1,'Min') & ...
val <= get(handles.slider1,'Max')
set(handles.slider1,'Value',val);