Create push button or state button component
collapse all in page
Syntax
btn = uibutton
btn = uibutton(parent)
btn = uibutton(style)
btn = uibutton(parent,style)
btn = uibutton(___,Name,Value)
Description
creates a push button in a new figure and returns the btn
= uibuttonButton
object. MATLAB® calls the uifigure function to create the figure.
example
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.btn
= uibutton(parent)
creates a button of the specified style. The button style can be btn
= uibutton(style)"push"
or "state"
.
example
creates a button of the specified style in the specified parent container.btn
= uibutton(parent,style)
example
creates a button with properties specified by one or more name-value arguments. For example, specify the button background color using the btn
= uibutton(___,Name,Value)BackgroundColor
property. Use this option with any of the input argument combinations in the previous syntaxes.
Examples
collapse all
Create Push Button
Create a push button in a UI figure.
fig = uifigure;b = uibutton(fig);
Create State Button
Open Live Script
Create a state button in a UI figure.
fig = uifigure;b = uibutton(fig,"state");
Click the button. The button remains in the pressed state after you click it.
Set and Access Button Property Values
Open Live Script
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]);
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;
Code Response to Button Click
Open Live Script
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 theButtonPushedFcn
callback property. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.
function plotAppfig = 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;endfunction 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.
Input Arguments
collapse all
style
— Style of button
"push"
(default) | "state"
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
— 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: 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.
If
style
is"push"
(default), see Button Properties.If
style
is"state"
, see StateButton Properties.
Version History
Introduced in R2016a
expand all
R2022b: Specify additional icon and icon alignment options
You can use these additional options when adding icons to a button:
Specify the
Icon
property as a predefined icon, such as'success'
.Specify the
IconAlignment
property as'leftmargin'
or'rightmargin'
to display the icon on the far left or far right of the button, regardless of the button text location.
R2020b: Wrap button 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.
See Also
Functions
- uifigure
Properties
- Button Properties | StateButton Properties
Tools
- App Designer
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
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:
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
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office