uibutton - Create push button or state button component - MATLAB (original) (raw)

Create push button or state button component

Syntax

Description

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

`btn` = uibutton([parent](#buimwfr-1%5Fsep%5Fmw%5Fa6330465-2069-40e4-857c-69da4d660066)) creates a button in the specified parent container. The parent can be a Figure object or one of its child containers.

example

`btn` = uibutton([style](#buimwfr-1-style)) creates a button of the specified style. The button style can be"push" or "state".

`btn` = uibutton([parent](#buimwfr-1%5Fsep%5Fmw%5Fa6330465-2069-40e4-857c-69da4d660066),[style](#buimwfr-1-style)) creates a button of the specified style in the specified parent container.

example

`btn` = uibutton(___,[Name,Value](#namevaluepairarguments)) 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.

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

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:

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:

Parent container, specified as a Figure object or one of its child containers: Tab, Panel, ButtonGroup, orGridLayout. 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 asName1=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

Enable text markup by setting the Interpreter property. Specify the interpreter as "html","latex", "tex", or"none".

You can use these additional options when adding icons to a button:

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 theWordWrap 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.