Main Content

uicheckbox

Create check box component

Description

cbx = uicheckbox creates a check box in a new figure window and returns the CheckBox object. MATLAB® calls the uifigure function to create the figure.

cbx = uicheckbox(parent) creates the check box in the specified parent container. The parent can be a Figure object created using the uifigure function or one of its child containers.

example

cbx = uicheckbox(___,Name,Value) specifies CheckBox properties using one or more name-value arguments. For example, uicheckbox("Value",1) creates a check box that is checked. Use this option with any of the input argument combinations in the previous syntaxes.

example

Examples

collapse all

Create a check box in a UI figure.

fig = uifigure;
cbx = uicheckbox(fig);

Figure contains an object of type uicheckbox.

Create a check box in a UI figure, and specify the check box text.

fig = uifigure;
cbx = uicheckbox(fig,"Text","Subscribe");

Figure contains an object of type uicheckbox.

Query the value of the check box.

val = cbx.Value
val = logical
   0

Programmatically select the check box by updating the Value property.

cbx.Value = 1;

Figure contains an object of type uicheckbox.

Create an app that allows a user to show and hide a legend in a plot.

In a file named toggleLegendApp.m, write a function that implements the app:

  • Create a UI figure and a grid layout manager to lay out the app.

  • Create a UI axes and a check box in the grid layout manager, and plot some data in the axes.

  • Write a callback function named checkBoxChanged that toggles the visibility of the plot legend, and assign the function to the ValueChangedFcn callback property of the check box. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.

function toggleLegendApp
fig = uifigure;
g = uigridlayout(fig,[2 3]);
g.RowHeight = {'1x','fit'};
g.ColumnWidth = {'1x','fit','1x'};

ax = uiaxes(g);
ax.Layout.Row = 1;
ax.Layout.Column = [1 3];
plot(ax,magic(3));
lgd = legend(ax,"hide");
cbx = uicheckbox(g,"Text","Show legend");
cbx.Layout.Row = 2;
cbx.Layout.Column = 2;

cbx.ValueChangedFcn = @(src,event) checkBoxChanged(src,event,lgd);
end

function checkBoxChanged(src,event,lgd)
val = event.Value;
lgd.Visible = val;
end

Run the toggleLegendApp function. Select the check box to show the legend.

UI figure window with axes showing some plotted data, a legend, and a selected "Show legend" check box below the axes

Input Arguments

collapse all

Parent container, specified as a Figure object created using the uifigure function or one of its child containers: Tab, Panel, ButtonGroup, or GridLayout. If you do not specify a parent container, MATLAB calls the uifigure function to create a new Figure object that serves as the parent container.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: uicheckbox(Value=1) specifies that the check box is displayed with a check mark.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: uicheckbox("Value",1) specifies that the check box is displayed with a check mark.

Note

The properties listed here are a subset of the available properties. For the full list, see CheckBox.

State of the check box, specified as 0 (false) or 1 (true). When the Value property is set to 1, the check box is checked. When the Value property is set to 0, the check box is not checked.

Value changed callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

This callback executes when the user selects or clears the check box in the app. The callback does not execute if the check box value changes programmatically.

This callback function can access specific information about the user’s interaction with the check box. MATLAB passes this information in a ValueChangedData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.PreviousValue returns the previous value of the check box. The ValueChangedData object is not available to callback functions specified as character vectors.

The following table lists the properties of the ValueChangedData object.

PropertyValue
ValueValue of check box after most recent app user interaction with it.
PreviousValueValue of check box before most recent app user interaction with it.
SourceComponent that executes the callback.
EventName'ValueChanged'

For more information about writing callbacks, see Callbacks in App Designer.

Location and size of the check box relative to the parent, specified as the vector [left bottom width height]. This table describes each element in the vector.

ElementDescription
leftDistance from the inner left edge of the parent container to the outer left edge of the check box
bottomDistance from the inner bottom edge of the parent container to the outer bottom edge of the check box
widthDistance between the right and left outer edges of the check box
heightDistance between the top and bottom outer edges of the check box

The Position values are relative to the drawable area of the parent container. The drawable area is the area inside the borders of the container and does not include the area occupied by decorations such as a menu bar or title.

All measurements are in pixel units.

Example: [200 200 102 15]

Version History

Introduced in R2016a

expand all

See Also

Functions

Properties

Tools