Main Content

uispinner

Create spinner component

Description

spn = uispinner creates a spinner in a new figure window and returns the Spinner object. MATLAB® calls the uifigure function to create the figure.

spn = uispinner(parent) creates the spinner in the specified parent container. The parent can be a Figure created using the uifigure function, or one of its child containers.

example

spn = uispinner(___,Name,Value) specifies Spinner properties using one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

example

Examples

collapse all

fig = uifigure;
spn = uispinner(fig);

Spinner in a UI figure window. The spinner has a value of 0 and buttons to increment and decrement the value.

Create a spinner in a panel.

fig = uifigure;
pnl = uipanel(fig);
spn = uispinner(pnl);

Spinner in a panel in a UI figure window

Create a spinner that limits the values the app user can enter to between 0 and 100, inclusive.

Create a spinner.

fig = uifigure;
spn = uispinner(fig);

Determine the limits. The returned values indicate that the lower and upper limits are unlimited.

limits = spn.Limits
limits =

  -Inf   Inf

Set the limits to 0 and 100.

spn.Limits = [0 100];

Create a spinner that allows the app user to enter a value greater than -5 and less than or equal to 10.

fig = uifigure;
spn = uispinner(fig,'Limits', [-5  10],...
            'LowerLimitInclusive','off',...
            'UpperLimitInclusive','on',...
            'Value', 5);

Run the code. If you enter a value in the spinner that is outside the limits, MATLAB automatically displays a message indicating the problem. MATLAB then restores the value to the previous valid value.

Create a spinner that allows the app user to enter any value, but always displays the value using exactly two decimals. Be aware that MATLAB stores the exact value that the app user enters.

fig = uifigure;
spn = uispinner(fig,'ValueDisplayFormat', '%.2f');

Run the code, and then enter 5.555 in the spinner. Click outside the spinner. The spinner displays 5.55.

MATLAB stores the original value, 5.555.

Click in the spinner, it displays the value originally typed.

Create a spinner and a slider. When an app user changes the spinner value, the slider updates to match that value.

Save the following code to spinnerValue.m on your MATLAB path. This code creates a figure window containing a slider and a spinner. When an app user changes the spinner value, the ValueChangedFcn updates the spinner to reflect the slider value.

function spinnervalue
fig = uifigure('Position',[100 100 370 280]);

sld = uislider(fig,...
    'Position',[90 220 120 3]);

spn = uispinner(fig,...
    'Position',[100 140 100 22],...
    'Limits',sld.Limits,...
    'ValueChangedFcn',@(spn,event) updateSlider(spn,sld));
end

% Create ValueChangedFcn callback
function updateSlider(spn,sld)
sld.Value = spn.Value;
end

Run spinnerValue.

Click and hold the up arrow in the spinner until the value reaches 24, and then release. The slider thumb moves to indicate the spinner value.

UI figure window with a slider and a spinner. Both the slider and the spinner have values of 24.

Create a spinner and a slider. As an app user changes the spinner value, the slider repeatedly updates to match that value.

Save the following code to showChangingValue.m on your MATLAB path. This code creates a figure window containing a slider and a spinner. As an app user changes the spinner value, the ValueChangingFcn repeatedly updates the slider to reflect the spinner value as it changes.

function showChangingValue
fig = uifigure('Position',[100 100 370 280]);
sld = uislider(fig,...
    'Position',[90 220 120 3]);

spn = uispinner(fig,...
    'Position',[100 140 100 22],...
    'Limits',sld.Limits,...
    'ValueChangingFcn',@(spn,event) spinnerChanging(event,sld));
end

% Create ValueChangingFcn callback
function spinnerChanging(event,sld)
sld.Value = event.Value;
end

Run showChangingValue.

Click, and hold the up arrow in the spinner until the value reaches 24, and then release. The slider moves as the spinner value changes.

UI figure window with a slider and a spinner. Both the slider and the spinner have values of 24.

Code the ValueChangedFcn callback to determine if the value is rising or falling compared to the previous spinner value. Set lamp color to green when the value is increasing and to red when the value is decreasing

Save the following code to upOrDown.m on your MATLAB path.

function upOrDown
fig = uifigure(...
    'Position',[100 100 190 170]);

lmp = uilamp(fig,...
    'Position',[90 50 20 20],...
    'Color','green');

spn = uispinner(fig,...
    'Position',[50 100 100 22],...
    'ValueChangedFcn',@(spn,event) spinnerValueChanged(event,lmp));
end

% Create ValueChangedFcn that uses event data
function spinnerValueChanged(event,lmp)
newValue = event.Value;
previousValue = event.PreviousValue;
difference = newValue-previousValue;
if difference > 0
    lmp.Color = 'green';
else
    lmp.Color = 'red';
end
end

Run upOrDown.

Each time you change the spinner value, the ValueChangedFcn determines whether the value is increasing or decreasing and sets the lamp color accordingly.

UI figure window with a spinner and a lamp. The value of the spinner is 8, and the lamp is red.

Input Arguments

collapse all

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.

Example: 'Value',150 specifies that the number 50 appears in the spinner.

The properties listed here are a subset of the available properties. For the full list, see Spinner.

Spinner value, specified as a numeric value.

When the app user types a value in the spinner, the value is a character vector. When the app user presses the Enter key or changes focus, MATLAB converts the app-user-entered value to a double-precision number.

MATLAB rejects the value if:

  • It cannot convert the character vector to a scalar number.

  • The value is NaN, blank, or a complex number.

  • The value is a mathematical expression, such as 1+2.

  • The value is less than or greater than the values specified by the Limits property.

When MATLAB rejects the app-user-entered value, a tooltip appears describing the value requirements. The spinner immediately reverts to its previous value and no ValueChangedFcn runs.

Example: 10

Data Types: double

Value display format, specified as a character vector or string scalar.

MATLAB uses sprintf to display the value using the specified format.

You can mix text with format operators. For example:

spin = uispinner('ValueDisplayFormat','%.0f MS/s');

The resulting spinner component looks like this:

Spinner with text "0 MS/s"

When the app user clicks in the spinner field, the field shows the value without the text.

Spinner with text "0"

For a complete list of supported format operators, see sprintf.

Rounding of fractional values entered by app users, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • 'on' — MATLAB rounds the value if it results in a valid value and executes the ValueChangedFcn callback. If the resulting value is outside the lower or upper Limits, then MATLAB rounds to the nearest value that falls within the Limits and then executes the callback.

  • 'off' — MATLAB does not round a fractional value to a whole number.

If the RoundFractionalValues property value changes from 'off' to 'on' programmatically, then MATLAB applies these rules:

  • If rounding the existing value yields an integer that lies inside the limit range specified by the Limits property, then MATLAB rounds up the existing value.

  • If rounding the existing value yields an integer that is less than the lower limit, then MATLAB rounds up the existing value.

  • If rounding the existing value yields an integer that is greater than the upper limit, then MATLAB rounds down the existing value.

  • If the limits are configured such that there is no valid integer in the range, then MATLAB sets the RoundFractionalValues property value back to 'off' and displays an error message.

Quantity by which the Value property increments or decrements when the app user presses the up and down arrows, respectively.

Minimum and maximum spinner values, specified as a two-element numeric array. The first value must be less than or equal to the second value. Set array elements to -Inf or Inf to specify no minimum or no maximum, respectively.

If you change Limits such that Value is less than the new lower limit, MATLAB sets Value to the lowest value within the new range. For example, suppose Limits is [0 100] and Value is 20. If Limits changes to [50 100], inclusive, then MATLAB sets Value to 50.

Similarly, if you change Limits such that the Value is greater than the new upper limit, then MATLAB sets Value to the new upper limit (assuming the limits are inclusive).

Example: [-Inf 200]

Example: [-100 Inf]

Example: [-100 200]

Data Types: double

Lower limit inclusiveness, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • 'on' — Value must be equal to or greater than the lower limit.

  • 'off' — Value must be greater than the lower limit.

Upper limit inclusiveness, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • 'on' — Value must be equal to or less than the upper limit.

  • 'off' — Value must be less than the upper limit.

For example, if you want the numeric input to be between 0 and 1, excluding 0 and 1, do all of the following:

  • Set the Limits property value to [0 1].

  • Set the UpperLimitInclusive property to 'off'.

  • Set the LowerLimitInclusive property to 'off'.

Value changed callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

This callback executes when the user changes focus or presses the Enter key after changing the spinner value. It does not matter whether the user changes the spinner value by typing or by pressing the arrow keys. The callback does not execute if the spinner value changes programmatically.

This callback function can access specific information about the user’s interaction with the spinner. MATLAB passes this information in a ValueChangedData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.PreviousValue returns the previous value of the spinner. The ValueChangedData object is not available to callback functions specified as character vectors.

The following table lists the properties of the ValueChangedData object.

PropertyValue
ValueValue of spinner after app user’s most recent interaction with it
PreviousValueValue of spinner before app user’s most recent interaction with it
SourceComponent that executes the callback
EventName'ValueChanged'

For more information about writing callbacks, see Callbacks in App Designer.

Value changing callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

This callback function executes as the user clicks and holds the up or down arrow on the spinner. It does not execute if the Value property changes programmatically.

This callback function can access specific information about the user’s interaction with the spinner. MATLAB passes this information in a ValueChangingData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.Value returns the current value of the spinner. The ValueChangingData object is not available to callback functions specified as character vectors.

The following table lists the properties of the ValueChangingData object.

PropertyValue
ValueCurrent value of the spinner as the app user is interacting with it
SourceComponent that executes the callback
EventName'ValueChanging'

The Value property of the Spinner is not updated until the app user releases the arrow key. Therefore, to get the values while the arrow key is being pressed, your code must get the Value property of the ValueChangingData object.

Note

Avoid updating the Value property of the Spinner object from within its own ValueChangingFcn callback, as this might result in unexpected behavior. To update the spinner value in response to user input, use a ValueChangedFcn callback instead.

The callback executes as follows:

  • If the app user clicks a spinner up or down arrow, the callback executes once. For example, suppose that the spinner value is 2, and the Step value is 1. If the app user clicks the up arrow, the callback executes.

  • If the app user presses and holds a spinner up or down arrow, the callback executes repeatedly. For example, if the app user clicks and holds the up arrow, the callback executes multiple times until the app user releases the up arrow.

For more information about writing callbacks, see Callbacks in App Designer.

Location and size of spinner relative to the parent container, specified as the vector [left bottom width height]. This table describes each element in the vector.

ElementDescription
leftDistance from the inner left edge of the parent container to the outer left edge of the spinner
bottomDistance from the inner bottom edge of the parent container to the outer bottom edge of the spinner
widthDistance between the right and left outer edges of the spinner
heightDistance between the top and bottom outer edges of the spinner

All measurements are in pixel units.

The Position values are relative to the drawable area of the parent container. The drawable area is the area inside the borders of the container and does not include the area occupied by decorations such as a menu bar or title.

Example: [100 100 100 22]

Version History

Introduced in R2016a

See Also

Functions

Properties

Tools