Creating Graphical User Interfaces |
Sharing Data Among a GUI's Callbacks
You can use GUI data, application data, and the UserData property to share data among a GUI's callbacks. See Mechanisms for Managing Data for information about these data forms.
GUI Data
GUI data is accessible to all callbacks of the GUI. A callback for one component can set a value in GUI data which can then be read by a callback for another component. If GUI data is a structure, then both callbacks must know the name of the field that is being used to store the data.
For GUIDE users, arguments that are passed from the command line to the opening function can be made available to the callbacks if the opening function adds them to the handles
structure.
Application Data
Application data can be associated with any object. In a GUI, if you asssociate it with the figure, all the GUI's callbacks can easily retrieve it by name, using the figure handle. You can also associate application data with individual components or menus.
To access application data, a calback must know the name of the data and the handle of the component with which it is associated.
Use the functions setappdata
, getappdata
, isappdata
, and rmappdata
to manage application data.
UserData Property
Use the get
function to retrieve application-defined data from a component's UserData
property. You must know the handle of the component to retrieve the data. If you are using GUIDE, you can retrieve the component handle from the handles
structure. If you have created your GUI programmatically, you can use the guihandles
function to generate a structure of handles for your GUI.
Example: Using GUI Data in a GUIDE-Generated GUI
This example uses GUI data to pass data between a slider and an editable text box in a GUI created using GUIDE. The GUI behavior is as follows:
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. Start by defining 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 use 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 sets 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 presses Enter or 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:
Adding Fields to a Data Structure | Properties That Control Component Behavior |
© 1994-2005 The MathWorks, Inc.