Main Content

uibutton

Create push button or state button component

Description

btn = uibutton creates a push button in a new figure and returns the Button object. MATLAB® calls the uifigure function to create the figure.

btn = uibutton(parent) creates a button in the specified parent container. The parent can be a figure created using the uifigure function or one of its child containers.

example

btn = uibutton(style) creates a button of the specified style. The button style can be "push" or "state".

btn = uibutton(parent,style) creates a button of the specified style in the specified parent container.

example

btn = uibutton(___,Name,Value) creates a button with properties specified by one or more name-value arguments. For example, specify the button background color using the BackgroundColor property. Use this option with any of the input argument combinations in the previous syntaxes.

example

Examples

collapse all

Create a push button in a UI figure.

fig = uifigure;
b = uibutton(fig);

Figure contains an object of type uibutton.

Create a state button in a UI figure.

fig = uifigure;
b = uibutton(fig,"state");

Figure contains an object of type uistatebutton.

Click the button. The button remains in the pressed state after you click it.

State button in a UI figure. The button is in a pressed state.

Create a state button in a UI figure, and customize its appearance by specifying property values.

fig = uifigure;
b = uibutton(fig,"state", ...
    "Text","Play", ...
    "Icon","play.png", ...
    "IconAlignment","top", ...
    "Position",[100 100 50 50]);

Figure contains an object of type uistatebutton.

Determine whether the state button is in its pressed state.

b.Value
ans = logical
   0

Programmatically update the button value so that it appears in its pressed state.

b.Value = true;

Figure contains an object of type uistatebutton.

Create an app that plots some data when an app user presses a button.

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

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

  • Create UI axes and a button in the grid layout manager.

  • Write a callback function named plotButtonPushed that plots some data in the UI axes, and assign the function to the ButtonPushedFcn callback property. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.

function plotApp
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];
b = uibutton(g, ...
    "Text","Plot Data", ...
    "ButtonPushedFcn", @(src,event) plotButtonPushed(ax));
b.Layout.Row = 2;
b.Layout.Column = 2;
end

function plotButtonPushed(ax)
x = linspace(0,2*pi,100);
y = sin(x);
plot(ax,x,y)
end

Run the plotApp function. Click the button to plot the data.

UI figure window with axes showing some plotted data and a Plot Data button below the axes

Input Arguments

collapse all

Style of button, specified as one of these values:

  • "push" — When clicked once, the button appears to press and release.

  • "state" — When clicked once, the button remains in the pressed or released state until it is clicked again.

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: uibutton(fig,BackgroundColor="blue")

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

Example: uibutton(fig,"BackgroundColor","blue")

Each type of button object supports a different set of properties. For a full list of properties and descriptions for each type, see the associated property page.

Version History

Introduced in R2016a

expand all

See Also

Functions

Properties

Tools