Create spinner component
creates a spinner in a
new figure window and returns the spn
= uispinnerSpinner
object.
MATLAB® calls the uifigure
function to create the
figure.
specifies spn
= uispinner(___,Name,Value
)Spinner
properties using one or more
Name,Value
pair arguments. Use this option with any of the
input argument combinations in the previous syntaxes.
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 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],... '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 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],... '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 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.
parent
— Parent containerFigure
object (default) | Panel
object | Tab
object | ButtonGroup
object | GridLayout
objectParent 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.
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
'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.
'Value'
— Spinner valueSpinner 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
'ValueDisplayFormat'
— Value display format'%11.4g'
(default) | character vector | string scalarValue 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:
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
.
'RoundFractionalValues'
— Rounding of fractional values'off'
(default) | on/off logical valueRounding 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.
'Step'
— Quantity by which value is incremented or decremented1
(default) | numeric scalarQuantity by which the Value
property increments
or decrements when the app user presses the up and down arrows, respectively.
'Limits'
— Minimum and maximum spinner valuesMinimum 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]
'LowerLimitInclusive'
— Lower limit inclusiveness'on'
(default) | on/off logical valueLower 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.
'UpperLimitInclusive'
— Upper limit inclusiveness'on'
(default) | on/off logical valueUpper 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'
.
'ValueChangedFcn'
— Value changed callback''
(default) | function handle | cell array | character vectorValue 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.
Property | Value |
---|---|
Value | Value of spinner after app user’s most recent interaction with it |
PreviousValue | Value of spinner before app user’s most recent interaction with it |
Source | Component that executes the callback |
EventName | 'ValueChanged' |
For more information about writing callbacks, see Write Callbacks in App Designer.
'ValueChangingFcn'
— Value changing callback''
(default) | function handle | cell array | character vectorValue 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.
Property | Value |
---|---|
Value | Current value of the spinner as the app user is interacting with it |
Source | Component 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.
For more information about writing callbacks, see Write Callbacks in App Designer.
'Position'
— Location and size of spinner[100 100 100 22]
(default) | [left bottom width height]
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.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the spinner |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the spinner |
width | Distance between the right and left outer edges of the spinner |
height | Distance 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]