MATLAB Creating Graphical User Interfaces

(Barry) #1

8 Programming a GUIDE UI


If you are creating a UI programmatically, (without GUIDE), then you can adapt the
example code into your code. To adapt an example into your code, omit the third input
argument, handles, from the function definition. Also, replace any references to the
handles array with the appropriate object handle. To associate the callback function
with the component, set the component's callback property to be a handle to the callback
function. For example, this command creates a push button component and sets the
Callback property to be a handle to the function, pushbutton1_callback.
pb =
uicontrol('Style','pushbutton','Callback',@pushbutton1_Callback);

Push Button


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

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
display('Goodbye');
close(gcf);

The first line of code, display('Goodbye'), displays the string, 'Goodbye', in the
Command Window. The next line gets a handle to the UI window using gcf and then
closes it.

Toggle Button


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

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

% Hint: get(hObject,'Value') returns toggle state of togglebutton1
button_state = get(hObject,'Value');
Free download pdf