Creating Graphical User Interfaces |
To obtain the string a user typed in an edit box, get the String
property in the callback.
function edittext1_Callback(hObject, eventdata, handles)
user_string = get(hObject,'string');
% proceed with callback...
For example, setting Max
to 2
, with the default value of 0
for Min
, enables users to select multiple lines
Obtaining 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...
Triggering Callback Execution
For both UNIX and Windows, clicking on the menu bar of the figure window causes the edit text callback to execute. Clicking on other components or the background of the GUI also executes the callback.
Check Boxes | Sliders |
© 1994-2005 The MathWorks, Inc.