Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

uispinner

(App Designer) Create spinner component

Use only with App Designer or figures created with the uifigure function. Apps created with GUIDE or the figure function do not support spinner components.

Syntax

spn = uispinner
spn = uispinner(parent)
spn = uispinner(___,Name,Value)

Description

spn = uispinner creates a spinner in a new UI figure window and returns the Spinner object.

example

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

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.

Input Arguments

collapse all

Parent object, specified as a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

collapse all

Spinner value, specified as a double-precision number.

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

Numeric display format, specified as a character vector.

MATLAB uses sprintf(NumericDisplayFormat,Value) to convert the value of the spinner to a character vector and displays it.

You can mix text with format operators. For example:

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

The resulting spinner component looks like this:

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

For a complete list of supported format operators, see sprintf in the MATLAB documentation.

Example: '%.0f MS/s'

Rounding of fractional values entered by app users, specified as one of the following:

  • '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 the second value. Set array elements to -Inf or Inf to specify no minimum or no maximum, respectively.

If you change Limits such that the Value property is outside the new limits, MATLAB sets the Value property to a value within the new limits. For example, suppose the Limits property is [0 100] and the Value property is 20. If the Limits property changes to [50 100], then MATLAB sets the Value property to 50 (assuming the LowerLimitInclusive value is 'on'.

Example: [-Inf 200]

Example: [-100 Inf]

Example: [-100 200]

Lower limit inclusiveness, specified as one of the following:

  • '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 one of the following:

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

Code to execute when the spinner value changes, specified as one of these values:

  • Function handle

  • Cell array containing a function handle and additional arguments

  • Character vector that is a valid MATLAB expression, which is evaluated in the base workspace.

This callback executes when the app 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 using the up and down arrow buttons.

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'

This callback function does not execute if the spinner value changes programmatically.

Code to execute as the spinner value changes, specified as one of these values:

  • Function handle

  • Cell array containing a function handle and additional arguments

  • Character vector that is a valid MATLAB expression, which is evaluated in the base workspace.

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.

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.

If the spinner Value property changes programmatically, then the callback does not execute.

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]

Examples

collapse all

fig = uifigure
spn = uispinner(fig);

Create a spinner in a panel.

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

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 UI 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
% Create UI figure window and components
fig = uifigure('Position',[100 100 350 275]);

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

% Create a spinner
spn = uispinner(fig,...
    'Position',[100 140 100 22],...
    '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.

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 UI 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
% Create UI figure window and components
fig = uifigure('Position',[100 100 350 275]);
sld = uislider(fig,...
    'Position',[90 220 120 3]);

spn = uispinner(fig,...
    'Position',[100 140 100 22],...
    '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.

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
% Create UI figure window
fig = uifigure(...
    'Position',[100 100 189 171]);

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

% Create Spinner
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.

See Also

Functions

Properties

Introduced in R2016a

Was this topic helpful?