User`s guide

9 Managing and Sh aring Application Data in GUIDE
The section “Sharing Data with GUI Data” on page 9-17 uses GUI data to
initialize and maintain an error counter. This example shows you how to do
the same thing using application data:
1 Define the error counter in the opening function by adding the following
code to the opening function:
% INITIALIZE ERROR COUNT AND USE APPDATA API TO STORE IT IN FIGURE.
slider_data.number_errors = 0;
setappdata(hObject,'slider',slider_data);
This code first creates a structure slider _data, and then assigns it to the
named application data
slider.ThehObject associates the application
data with the figure, because this code appears in the opening function.
2 Convert the slider Value property to a string and set the value of the edit
text component’s
String property from the slider callback b y adding this
statement to the callback:
set(handles.edittext1,'String',...
num2str(get(hObject,'Value')));
Because this statement appears i n the slider callback, hObject is the
handle of the slider.
3 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 it must increment the count. The code
then saves the application data and displays the new error count.
val = str2double(get(hObject,'Stri ng'));
% 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 th e error count.
9-16