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(style) creates a button of the specified style.

example

btn = uibutton(parent) creates the 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(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 pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create a push button.

fig = uifigure;
btn = uibutton(fig);

UI figure window with a push button. The default button text is Button.

Create a state button by specifying the style as 'state'.

fig = uifigure;
btn = uibutton(fig,'state');

UI figure window with a push button. The default button text is State Button.

fig = uifigure('Name','My Figure');
pnl = uipanel(fig);
btn = uibutton(pnl); 

UI figure window with a panel and a button. The button is in the panel.

Create a state button and specify property values.

fig = uifigure;
btn = uibutton(fig,'state',...
               'Text', 'Record',...
               'Value', true,...
               'Position',[50,100, 100, 22]);

UI figure window with a state button. The button text is Record. The button is dark gray.

Determine the font name of the state button text.

fname = btn.FontName
fname =

Helvetica

Change the font name of the button text.

btn.FontName = 'Arial Narrow';

Create a button and a UI axes. When the app user presses the button, a graph is created.

Create buttonPlot.m on your MATLAB path. This code creates a window containing a button and a UI axes. When the app user clicks the button, the ButtonPushedFcn plots some data.

function buttonPlot
% Create a figure window
fig = uifigure;

% Create a UI axes
ax = uiaxes('Parent',fig,...
            'Units','pixels',...
            'Position', [104, 123, 300, 201]);   

% Create a push button
btn = uibutton(fig,'push',...
               'Position',[420, 218, 100, 22],...
               'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn,ax));
end

% Create the function for the ButtonPushedFcn callback
function plotButtonPushed(btn,ax)
        x = linspace(0,2*pi,100);
        y = sin(x);
        plot(ax,x,y)
end

Run buttonPlot, and then click the push button. MATLAB plots the data.

An app inside a UI figure. The app contains a plot with some data, and a button to the right of the plot.

Input Arguments

collapse all

Style of button, specified as one of the following:

  • '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.

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

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