
8 Programming the GUI
8-20
function pushbutton1_Callback(hObject, eventdata, handles)
display Goodbye
delete(handles.figure1);
Adding an Image to a Push Button or Toggle Button
To add an image to a push button or toggle button, assign the button’s CData
property an m-by-n-by-3 array of RGB values that defines a truecolor image.
For example, the array
a defines 16-by-64 truecolor image using random values
between 0 and 1 (generated by
rand).
a(:,:,1) = rand(16,64);
a(:,:,2) = rand(16,64);
a(:,:,3) = rand(16,64);
set(hObject,'CData',a)
To add the image when the button is created, add the code to the button’s
CreateFcn callback. You may want to delete the value of the button’s String
property, which would usually be used as a label.
See
ind2rgb for information on converting a matrix X and corresponding
colormap map, i.e., an
(X, MAP) image, to RGB (truecolor) format.
Toggle Button
The callback for a toggle button needs to query the toggle button to determine
what state it is in. MATLAB sets the
Value property equal to the Max property
when the toggle button is pressed (
Max is 1 by default) and equal to the Min
property when the toggle button is not pressed (
Min is 0 by default).
The following code illustrates how to program the callback in the GUI M-file.
function togglebutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
% Toggle button is pressed-take approperiate action
...
Komentarze do niniejszej Instrukcji