User`s guide

10 Examples of GUIDE GUIs
if isnan(f1) || ~isreal(f1)
% isdouble returns NaN for non-numbers and f1 cannot be complex
% Disable the Plot button and change its string to say why
set(handles.plot_button,'String','Cannot plot f1')
set(handles.plot_button,'Enable','off')
% Give the edit text box focus so user can correct the error
uicontrol(hObject)
else
% Enable the Plot button with its original name
set(handles.plot_button,'String','Plot')
set(handles.plot_button,'Enable','on')
end
Similar code validates the f2 input.
Thetimevectorinput,
t, is more complicated to validate. As the str2double
function does not operate on vectors, the eval function is called to convert the
input string into a MATLAB expression. Because a user can type many things
that
eval cannot handle, the first task is to make sure that eval succeeded.
The
t_input_Callback us es try and catch blocks to do the following:
Call
eval with the t_input string inside the try block.
If
eval succeeds, perform a dditional tests within the try block.
If
eval generates an error, pass control to the catch block.
In that block, the callback disables the Plot button and changes its
String
to 'Cannot plot t'.
The remaining code in the
try block makes sure that the variable t returned
from
eval is a monotonically increasing vector of numbers with no more than
1000 elements. If
t passes all these tests, the callback enables Plot button
and sets its
String to 'Plot'. If it fails any of the tests, the callback disables
the Plot button and changes its
String to an appropriate short message.
Here are the
try and catch blocks from the callback:
% Disable the Plot button ... until proven innocent
set(handles.plot_button,'Enable','off')
try
t = eval(get(handles.t_input,'String'));
if ~isnumeric(t)
10-12