Skip to Main Content Skip to Search
Product Documentation

uicontrol - Create user interface control object

Syntax

handle = uicontrol('Name',Value,...)
handle = uicontrol(parent,'Name',Value,...)
handle = uicontrol
uicontrol(uich)

Description

handle = uicontrol('Name',Value,...) creates a uicontrol and assigns the specified properties and values to it. It assigns the default values to any properties you do not specify. The default uicontrol style is pushbutton. The default parent is the current figure. See the Uicontrol Properties reference page for more information.

handle = uicontrol(parent,'Name',Value,...) creates a uicontrol in the object specified by the handle, parent. If you also specify a different value for the Parent property, the value of the Parent property takes precedence. parent can be the handle of a figure, uipanel, or uibuttongroup.

handle = uicontrol creates a push button in the current figure. The uicontrol function assigns all properties their default values.

uicontrol(uich) gives focus to the uicontrol specified by the handle, uich.

uicontrol creates a uicontrol graphics objects (user interface controls), which you use to implement graphical user interfaces.

Specifying the Uicontrol Style

When selected, most uicontrol objects perform a predefined action. To create a specific type of uicontrol, set the Style property as one of the following strings:

Tips

Examples

Create a figure and an axes to contain a 3-D surface plot.

    figure
    hax = axes('Units','pixels');
    surf(peaks)

Place a uicontrol object to let users change the colormap with a pop-up menu. Supply a function handle as the object's Callback property:

uicontrol('Style', 'popup',...
           'String', 'jet|hsv|hot|cool|gray',...
           'Position', [20 340 100 50],...
           'Callback', @setmap);       % Popup function handle callback
                                       % Implemented as a subfunction

Add a different uicontrol. Create a push button that clears the current axes when pressed. Position the button inside the axes at the lower left. All uicontrols have default units of pixels. In this example, the axes has units of pixels, as well.

uicontrol('Style', 'pushbutton', 'String', 'Clear',...
        'Position', [20 20 50 20],...
        'Callback', 'cla');        % Pushbutton string callback
                                   % that calls a MATLAB function

Add a slider uicontrol to control the vertical scaling of the surface object. Position it at the bottom right corner of the figure. Then add a text uicontrol as a label for the slider.

uicontrol('Style', 'slider',...
        'Min',1,'Max',50,'Value',41,...
        'Position', [400 20 120 20],...
        'Callback', {@surfzlim,hax});   % Uses cell array function handle callback
                                    % Implemented as a subfunction with an argument
uicontrol('Style','text',...
        'Position',[400 45 120 20],...
        'String','Vertical Exaggeration')

To operate the controls, you need callback functions. The callback for the pop-up menu is a subfunction called setmap that contains the following code.

    function setmap(hObj,event) %#ok<INUSD>
        % Called when user activates popup menu 
        val = get(hObj,'Value');
        if val ==1
            colormap(jet)
        elseif val == 2
            colormap(hsv)
        elseif val == 3
            colormap(hot)
        elseif val == 4
            colormap(cool)
        elseif val == 5
            colormap(gray)
        end
    end

The callback for the slider is also a subfunction in the same file, and contains the following code.

function surfzlim(hObj,event,ax) %#ok<INUSL>
    % Called to set zlim of surface in figure axes
    % when user moves the slider control
    val = 51 - get(hObj,'Value');
    zlim(ax,[-val val]);
end

To use the uicontrols in the example, you need to put all the functions in a MATLAB code file. Copy the following code and paste it into a new file. Save the file as ex_uicontrol.m on your search path, and then run it.

function ex_uicontrol
    % Example code for uicontrol reference page

    % Create a figure and an axes to contain a 3-D surface plot.
    figure
    hax = axes('Units','pixels');
    surf(peaks)
    % Create a uicontrol object to let users change the colormap
    % with a pop-up menu. Supply a function handle as the object's 
    % Callback:
    uicontrol('Style', 'popup',...
           'String', 'hsv|hot|cool|gray',...
           'Position', [20 340 100 50],...
           'Callback', @setmap);       % Popup function handle callback
                                       % Implemented as a subfunction
    
    % Add a different uicontrol. Create a push button that clears 
    % the current axes when pressed. Position the button inside
    % the axes at the lower left. All uicontrols have default units
    % of pixels. In this example, the axes does as well.
    uicontrol('Style', 'pushbutton', 'String', 'Clear',...
        'Position', [20 20 50 20],...
        'Callback', 'cla');        % Pushbutton string callback
                                   % that calls a MATLAB function

    % Add a slider uicontrol to control the vertical scaling of the
    % surface object. Position it under the Clear button.
    uicontrol('Style', 'slider',...
        'Min',1,'Max',50,'Value',41,...
        'Position', [400 20 120 20],...
        'Callback', {@surfzlim,hax});   % Slider function handle callback
                                        % Implemented as a subfunction
   
    % Add a text uicontrol to label the slider.
    uicontrol('Style','text',...
        'Position',[400 45 120 20],...
        'String','Vertical Exaggeration')
end


function setmap(hObj,event) %#ok<INUSD>
    % Called when user activates popup menu 
    val = get(hObj,'Value');
    if val ==1
        colormap(jet)
    elseif val == 2
        colormap(hsv)
    elseif val == 3
        colormap(hot)
    elseif val == 4
        colormap(cool)
    elseif val == 5
        colormap(gray)
    end
end

function surfzlim(hObj,event,ax) %#ok<INUSL>
    % Called to set zlim of surface in figure axes
    % when user moves the slider control 
    val = 51 - get(hObj,'Value');
    zlim(ax,[-val val]);
end

See Also

textwrap | uibuttongroup | uimenu | uipanel | uipushtool | uitable | uitoggletool

How To

  


Free MATLAB Interactive Kit

Explore how to use MATLAB to make advancements in engineering and science.


Download free kit

Trials Available

Try the latest version of MATLAB and other MathWorks products.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS