User`s guide
GUI with Multiple Axes (GUIDE)
Validate User Input as Numbers
GUI u sers type parameters into three edit text boxes as strings of text. If
they type an inappropriate number or something that is not a number, the
graphs can fail to inform or even to generate. P reventing bad inputs from
being processed is an important function of almost any GUI that performs
computations. In this GUI, it is important that:
• All three inputs are positive or negative real numbers.
• The
t (time) input is a vector that increases m onotonically and i s not too
long to legibly display.
You can make a GUI respond in a variety of ways to inappropriate inputs.
These include:
• Clearing an invalid input, forcing the user to enter another one.
• Rep lac in g a n in v alid in pu t w ith i ts last previous v alid v alu e o r its de fau lt
value.
• Disabling controls that initiate processing of the input.
• Displaying an error alert or playing a sound.
You can combine these actions, as appropriate. In this example, each of the
edit text control callbacks validates its own input. If the input fails validation,
the callback disables the Plot button, changes its
String to indicate the
type of problem encountered, and restores focus to the edit text control,
highlighting the erroneous input. As soon as the user re-enters a value that
is acceptable, the Plot button is enabled with its
String set back to ' Plot'.
This approach prevents plotting errors and avoids the need for an error dialog.
Validating the
f1 and f2 inputs is not difficult. These inputs mu st be real
scalar numbers that can be positive or negative. The
str2double function
handles most cases, returning
NaN (Not a N umber) for nonnumeric or
nonscalar string expressions. An additional test using the
isreal function
makes sure that the user has not entered a complex number, such as
'4+2i'.
The
f1_input_Callback contains the following code to validate user input
for
f1 :
f1 = str2double(get(hObject,'String'));
10-11