| MATLAB® | ![]() |
| On this page… |
|---|
All GUI callbacks can access GUI data. A callback for one component can set a value in GUI data, which, a callback for another component can then read.
Note For more information about GUI data, see GUI Data . |
This example uses a GUI that contains a slider and an edit text component as shown in the following figure. A static text component instructs the user to enter a value in the edit text or click the slider. This example uses GUI data to initialize and maintain an error counter.

The GUI behavior is as follows:
When a user moves the slider, the edit text component displays the slider's current value.
When a user types a value into the edit text component, the slider updates to this value.
If a user enters a value in the edit text that is out of range for the slider (a value that is not between 0 and 1), the application returns a message in the edit text component that indicates the number of times the user entered an incorrect value.
The commands in the following steps initialize the error counter and implement the interchange between the slider and the edit text component:
Define the error counter in the opening function. The GUI records the number of times a user enters an incorrect value in the edit text component and stores this number in a field of the handles structure.
Define the number_errors field in the opening function as follows:
handles.number_errors = 0;
Type the following line, which GUIDE automatically inserts into the opening function:
guidata(hObject,handles); % Save the updated handles structure.
The guidata command saves the modified handles structure so that it can be retrieved in the GUI's callbacks.
Set the value of the edit text component String property from the slider callback. The following command in the slider callback updates the value displayed in the edit text component when a user moves the slider and releases the mouse button:
set(handles.edittext1,'String',... num2str(get(handles.slider1,'Value')));
This 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.
Set the slider value from the edit text component's callback. The edit text component's callback sets the slider's value to the number the user enters, after checking to see if it is a single numeric value between 0 and 1. If the value is out of range, the error count incremens and the edit text displays a message telling the user how many times they entered an invalid number. Because this code appears in the edit text component's callback, hObject is the handle of the edit text component:
val = str2double(get(hobject,'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(hobject,'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 text component and then presses Enter or clicks outside the edit text, 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 in the edit text component:

You can associate application data with any object—a component, menu, or the figure itself. To access application data, a callback must know the name of the data and the handle of the component with which it is associated. Use the setappdata, getappdata, isappdata, and rmappdata functions to manage application data.
Note For more information about application data, see Application Data . |
The section, GUI Data Example: Passing Data Between Components uses GUI data to initialize and maintain an error counter. This example shows you how to do the same thing using application data:
Define the error counter in the opening function by adding the following code to the opening function:
slider_data.number_errors = 0; setappdata(hObject,'slider',slider_data);
This code first creates a structure slider_dataand then assigns it to the named application data slider. The hObject associates the application data with the figure, because this code appears in the opening function.
Convert the slider Value property to a string and set the value of the edit text component's String property from the slider callback by adding this statement to the callback:
set(handles.edittext1,'String',num2str(get(hObject,'Value')));
Because this statement appears in the slider callback, hObject is the handle of the slider.
Set the slider value from the edit text component's callback. Add the following code to the callback. It assumes the figure's Tag property is figure1.
To update the number of errors, this code must first retrieve the named application data slider, and then increment the count. It then saves the application data and displays the new error count.
val = str2double(get(hObject,'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 % Retrieve and increment the error count. slider_data = getappdata(handles.figure1,'slider'); slider_data.number_errors = slider_data.number_errors+1; % Save the changes. setappdata(handles.figure1,'slider',slider_data); % Display new total. set(hObject,'String',... ['You have entered an invalid entry ',... num2str(slider_data.number_errors),' times.']); end
Every GUI component, and the figure itself, has a UserData property that you can use to store application-defined data. To access UserData, a callback must know the handle of the component with which the property is associated.
Use the get function to retrieve UserData, and the set function to set it.
Note For more information about UserData Property see UserData Property |
The section, GUI Data Example: Passing Data Between Components, uses GUI data to initialize and maintain an error counter. This example shows you how to use the edit text component's UserData property to store the error count.
Add the following code to the opening function to initialize the edit text component's UserData property. This code initializes the data in a structure to allow for other data that may be needed.
data.number_errors = 0; set(handles.edittext1,'UserData',data.number_errors)
Add the following statement to set the edit text value from the slider callback:
set(handles.edittext1,'String',... num2str(get(hObject,'Value')));
where hObject is the handle of the slider.
Add the following lines of code to the slider callback to set the slider value from the edit text callback:
val = str2double(get(hObject,'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 % Retrieve and increment the error count. data = get(hObject,'UserData'); data.number_errors = data.number_errors+1; % Save the changes. set(hObject,'UserData',data); % Display new total. set(hObject,'String',... ['You have entered an invalid entry ',... num2str(number_errors),' times.']); end
To update the number of errors, the code must first retrieve the value of the edit text UserData property, and then increment the count. It then saves the updated error count in the UserData property and displays the new count.
hObject is the handle of the edit text component because this code appears in the edit text callback.
![]() | Mechanisms for Managing Data | Making Multiple GUIs Work Together | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |