
8 Programming the GUI
8-22
function checkbox1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))
% Checkbox is checked-take approriate action
else
% Checkbox is not checked-take approriate action
end
Edit Text
To obtain the string a user typed in an edit box, get the String property in the
the
Callback callback.
function edittext1_Callback(hObject, eventdata, handles)
user_string = get(hObject,'String');
% Proceed with callback
If the edit text Max and Min properties are set such that Max - Min > 1, the
user can enter multiple lines. For example, setting
Max to 2, with the default
value of
0 for Min, enables users to enter multiple lines.
Retrieving Numeric Data from an Edit Text Component
MATLAB returns the value of the edit text String property as a character
string. If you want users to enter numeric values, you must convert the
characters to numbers. You can do this using the
str2double command, which
converts strings to doubles. If the user enters nonnumeric characters,
str2double returns NaN.
You can use the following code in the edit text callback. It gets the value of the
String property and converts it to a double. It then checks whether the
converted value is
NaN (isnan), indicating the user entered a nonnumeric
character and displays an error dialog (
errordlg).
function edittext1_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'string'));
if isnan(user_entry)
errordlg('You must enter a numeric value','Bad Input','modal')
end
% Proceed with callback...
Commenti su questo manuale