MATLAB Creating Graphical User Interfaces

(Barry) #1
Callbacks for Specific Components

To enable users to enter multiple lines of text, set the Max and Min properties to numeric
values that satisfy Max - Min > 1. For example, set Max to 2 , and Min to 0 to satisfy
the inequality. In this case, the callback function triggers when the end user clicks on an
area in the UI that is outside of the text field.


Retrieve Numeric Values


If you want to interpret the contents of an edit text field as numeric values, then convert
the characters to numbers using the str2double function. The str2double function
returns NaN for nonnumeric input.


This code is an example of an edit text field callback function that interprets the user’s
input as numeric values.


function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents as a double
input = str2double(get(hObject,'string'));
if isnan(input)
errordlg('You must enter a numeric value','Invalid Input','modal')
uicontrol(hObject)
return
else
display(input);
end


When the end user enters values into the edit text field and presses the Enter key, the
callback function gets the value of the String property and converts it to a numeric
value. Then, it checks to see if the value is NaN (nonnumeric). If the input is NaN, then
the callback presents an error dialog box.


Slider


This code is an example of a slider callback function in GUIDE. Associate this function
with the slider Callback property to make it execute when the end user moves the
slider.


function slider1_Callback(hObject, eventdata, handles)

Free download pdf