
Sharing Data Among a GUI’s Callbacks
9-9
3 Set the slider value from the edit text Callback 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 in the edit
text box:
Application Data
Application data can be associated 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
Komentáře k této Příručce