uispinner
Create spinner component
Description
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.
Examples
Create Spinner in Figure Window
fig = uifigure; spn = uispinner(fig);
Create Spinner in Panel
Create a spinner in a panel.
fig = uifigure; pnl = uipanel(fig); spn = uispinner(pnl);
Set and Access Spinner Property Values
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 Spinner and Specify Limit Inclusiveness
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 Spinner That Displays Values Using Exactly Two Decimals
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.
Code Response to Changed Spinner Value
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.
Code Response to Changing 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],... '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.
Code Response to Calculate Changed Spinner Value
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.
Input Arguments
parent
— Parent container
Figure
object (default) | Tab
object | Panel
object | ButtonGroup
object | GridLayout
object
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
.
Value
— Spinner value
0 (default) | numeric value
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
ValueDisplayFormat
— Value display format
'%11.4g'
(default) | character vector | string scalar
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:
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 value
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 theValueChangedFcn
callback. If the resulting value is outside the lower or upperLimits
, then MATLAB rounds to the nearest value that falls within theLimits
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 decremented
1
(default) | numeric scalar
Quantity by which the Value
property increments
or decrements when the app user presses the up and down arrows, respectively.
Limits
— Minimum and maximum spinner values
[-Inf Inf]
(default) | two-element numeric array
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
LowerLimitInclusive
— Lower limit inclusiveness
'on'
(default) | on/off logical value
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.
UpperLimitInclusive
— Upper limit inclusiveness
'on'
(default) | on/off logical value
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'
.
ValueChangedFcn
— Value changed callback
''
(default) | function handle | cell array | character vector
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.
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 Callbacks in App Designer.
ValueChangingFcn
— Value changing callback
''
(default) | function handle | cell array | character vector
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.
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.
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.
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]
Version History
Introduced in R2016a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)