| Creating Graphical User Interfaces | ![]() |
Toggle buttons generate an action and indicate a binary state (e.g., on or off). When you click on a toggle button, it appears depressed and remains depressed until you release the mouse button, at which point the callback executes. A subsequent mouse click returns the toggle button to the raised state and again executes its callback.
Programming the Callback
The callback routine 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 depressed (Max is 1 by default) and equal to the Min property when the toggle button is not depressed (Min is 0 by default).
From the GUI M-file
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 pressedelseif button_state == get(hObject,'Min')% toggle button is not pressedend
Adding an Image to a Push Button or Toggle Button
Assign the CData property an m-by-n-by-3 array of RGB values that define a truecolor image. For example, the array a defines 16-by-128 truecolor image using random values between 0 and 1 (generated by rand).
See ind2RGB for information on converting an (X, MAP) image to an RGB image.
| Push Buttons | Radio Buttons | ![]() |