Creating Graphical User Interfaces |
Managing GUI Data with the Handles Structure
Note
GUI data is application-defined data that is saved and retrieved using the guidata function. GUIDE uses guidata to manage the handles structure.
|
GUIDE provides a mechanism, called the handles structure, for storing and retrieving shared data using the same structure that contains the GUI component handles.
This structure, which initially contains only the handles of the components in the GUI, is passed to the handles
argument of each callback in the GUI M-file. Because this structure is passed to all callbacks for the GUI, any data you add to it become available to all the callbacks. The following example illustrates this technique.
If you are not familiar with MATLAB structures, see Structures for more information.
Example: Passing Data Between Callbacks
This example demonstrates how to use the handles
structure to pass data between callbacks. The example passes data between a slider and an editable text box in the following way:
The following figure shows the GUI with a static text field above the edit text box.
Defining the Data Fields 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. You can define this field, called number_errors
, in the opening function as follows:
Type this command before the following line, which GUIDE automatically inserts into the opening function.
The guidata
command saves the handles
structure so that it can be retrieved in the callbacks.
Note
To save any changes that you make to the handles structure, you must add the command guidata(hObject, handles) following the code that implements the changes.
|
Setting the Edit Text Value from the Slider 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.
The code combines three commands:
get
command obtains the current value of the slider.
num2str
command converts the value to a string.
set
command resets the String
property of the edit text to the updated value.
Setting the Slider Value from the Edit Text Callback
The edit text callback sets the slider's value to the number the user types in, after checking to see if it is a single numeric value between 0 and 1. If the value is out of range, then the error count is incremented and the edit text displays a message telling the user how many times they have entered an invalid number.
val = str2double(get(handles.edit1,'String'));% Determine whether val is a number between 0 and 1
if isnumeric(val) & length(val)==1 & ... val >= get(handles.slider1,'Min') & ... val <= get(handles.slider1,'Max') set(handles.slider1,'Value',val); else% Increment the error count, and display it
handles.number_errors = handles.number_errors+1; guidata(hObject,handles);% store the changes
set(handles.edit1,'String',... ['You have entered an invalid entry ',... num2str(handles.number_errors),' times.']); end
If the user types a number between 0 and 1 in the edit box and then clicks outside the edit box, the callback sets handles.slider1
to the new value and the slider moves to the corresponding position.
If the entry is invalid -- for example, 2.5
-- the GUI increments the value of handles.number_errors
and displays a message like the following:
Figures | Managing Application Data |
© 1994-2005 The MathWorks, Inc.