User`s guide
12 Code a Programmatic GUI
Retrieve Numeric Data from an Edit Text Component. MATLA B
software 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 code similar to the following in an 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 box (
errordlg).
function editte xt1_ Callback(hObject, e vent data, handles)
user_entry = str2double(get(hObject,'string'));
if isnan(use r_en try)
errordlg('You must enter a numeric value','Bad Input','modal')
uicontrol(hObject)
return
end
% Proceed with callback...
Edit text controls lose focus when the user c ommits and edit (by typing
Return or clicking away). The line
uicontrol(hObject) restores focus to the
edit text box. Although doing this is not needed for its callback to work, it is
helpful in the event that user input fails validation. The com mand has the
effect of selecting all the text in the edit text box.
Trigger Callback Execution. If the contents of the edit text component
have been changed, clicking inside the GUI, but outside the edit text, causes
theedittextcallbacktoexecute. TheusercanalsopressEnter for an edit
text that allows only a single line of text, or Ctrl+Enter for an edit text that
allows multiple lines.
Availa ble Keyboard Accelerators. GUI users can use the following
keyboard accelerators to modify the content of an edit text. These accelerators
are not modifiable.
• Ctrl+X –Cut
• Ctrl+C –Copy
12-22