
- •Creating Graphical User Interfaces Sharing Data with the Handles Structure
- •Functions and Callbacks in the m-File
- •Opening Function
- •Output Function
- •Callbacks
- •Input and Output Arguments
- •Programming Callbacks for gui Components
- •Toggle Button Callback
- •Radio Buttons
- •Check Boxes
- •Edit Text
- •Sliders
- •List Boxes
- •Pop-Up Menus
- •Button Groups
- •ActiveX Controls
- •Figures
- •Managing gui Data with the Handles Structure
- •Example: Passing Data Between Callbacks
- •Application Data
- •Functions for Accessing Application-Defined Data
Programming Callbacks for gui Components
This section explains how to program the callbacks for some specific GUI components. Callback Properties describes the different kinds of callbacks.
See a component's property reference page to determine which callbacks apply for that component. Setting Component Properties -- The Property Inspector provides links to the property reference pages.
This section provides information on the following topics:
Toggle Button Callback
Radio Buttons
Check Boxes
Edit Text
Sliders
List Boxes
Pop-Up Menus
Panels
Button Groups
Axes
ActiveX Controls
Figures
Toggle Button Callback
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 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 pressed
elseif button_state == get(hObject,'Min')
% toggle button is not pressed
end
Note If you have toggle buttons that are managed by a button group component, you must include the code to control them in the button group's SelectionChangeFcn callback function, not in the individual toggle button Callback functions. A button group overwrites the Callback properties of radio buttons and toggle buttons that it manages. See Button Groups for more information.
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).
a(:,:,1) = rand(16,128);
a(:,:,2) = rand(16,128);
a(:,:,3) = rand(16,128);
set(hObject,'CData',a)
See ind2rgb for information on converting an (X, MAP) image to an RGB image.
Radio Buttons
You can determine the current state of a radio button from within its callback by querying the state of its Value property, as illustrated in the following example:
if (get(hObject,'Value') == get(hObject,'Max'))
% then rsdio button is selected-take approriate action
else
% radio button is not selected-take approriate action
end
Note If you have radio buttons that are managed by a button group component, you must include the code to control them in the button group's SelectionChangeFcn callback function, not in the individual radio button Callback functions. A button group overwrites the Callback properties of radio buttons and toggle buttons that it manages. See Button Groups for more information
.
Check Boxes
You can determine the current state of a check box from within its callback by querying the state of its Value property, as illustrated in the following example:
function checkbox1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Max'))
% then checkbox is checked-take approriate action
else
% checkbox is not checked-take approriate action
end