uicheckbox
Create check box component
Description
creates a check box in
a new figure window and returns the cbx
= uicheckboxCheckBox
object. MATLAB® calls the uifigure
function to create the
figure.
specifies cbx
= uicheckbox(___,Name,Value
)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.
Examples
Create Check Box
Set and Access Check Box Property Values
Create a check box in a UI figure, and specify the check box text.
fig = uifigure; cbx = uicheckbox(fig,"Text","Subscribe");
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;
Code Response to Check Box Selection
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 theValueChangedFcn
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.
Input Arguments
parent
— Parent container
Figure
object (default) | Tab
object | Panel
object | ButtonGroup
object | GridLayout
object
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
.
Value
— State of check box
0 (default) | 1
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.
ValueChangedFcn
— Value changed callback
[]
(default) | function handle | cell array | character vector
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.
Property | Value |
---|---|
Value | Value of check box after most recent app user interaction with it. |
PreviousValue | Value of check box before most recent app user interaction with it. |
Source | Component that executes the callback. |
EventName | 'ValueChanged' |
For more information about writing callbacks, see Callbacks in App Designer.
Position
— Location and size of check box
[100 100 84 22]
(default) | [left bottom width height]
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.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the check box |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the check box |
width | Distance between the right and left outer edges of the check box |
height | Distance 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 R2016aR2020b: Wrap check box text
Use the WordWrap
property to prevent text from getting
clipped horizontally when the width of the UI component is smaller than the text you
want to display. Setting the WordWrap
property to
'on'
breaks the text into new lines so that each line fits
within the component. It avoids breaking words when possible. When the property is
set to 'off'
, the text does not wrap.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)