uilistbox
Create list box component
Description
creates a list box in a
new figure window and returns the lb
= uilistboxListBox
object.
MATLAB® calls the uifigure
function to create the
figure.
creates the list box with properties specified by one or more name-value arguments.
Use this option with any of the input argument combinations in the previous
syntaxes. For example, lb
= uilistbox(___,Name,Value
)uilistbox("Multiselect","on")
creates a
list box that allows an app user to select multiple items.
Examples
Create List Box
Set and Access List Box Property Values
Create a list box in a UI figure, and specify the list box items.
fig = uifigure; lb = uilistbox(fig,"Items",["Australia","France","Germany"]);
Query the value of the selected item.
val = lb.Value
val = 'Australia'
Programmatically update the list box selection.
lb.Value = "Germany";
Create List Box That Allows Selection of Multiple Items
Create a list box in a UI figure. Allow the app users to select multiple items.
fig = uifigure; lb = uilistbox(fig,"Multiselect","on");
Select multiple items in the list box by holding Ctrl while clicking.
The Value
property stores all selected items as a
cell array.
val = lb.Value
val = 1×2 cell array {'Item 2'} {'Item 4'}
Associate Data with List Box Items
Create a list box in a UI figure, and specify a list of color names that appear in the list box by setting the Items
property.
fig = uifigure; lb = uilistbox(fig,"Items",["Red","Green","Blue"]);
When there is no data associated with the items, the Value
property of the list box is an element of Items
.
val = lb.Value
val = 'Red'
Associate hex color data with the list box items by setting the ItemsData
property. Setting ItemsData
does not change how the items are displayed to the app user.
lb.ItemsData = ["#F00","#0F0","#00F"];
When the ItemsData
property is nonempty, the Value
property of the list box is an element of ItemsData
.
val = lb.Value
val = "#F00"
Specifying ItemsData
can make it easier to perform operations associated with the selected item. For example, plot some data in the selected color by passing the hex color value directly to the plot
function.
plot(1:10,"Color",lb.Value)
Code Response to List Box Selection
Create an app that updates the colormap of a chart when a user selects a list box item.
In a file named colormapApp.m
, write a function that implements the app:
Create a UI figure and a grid layout manager to lay out the app.
Create a list box and UI axes with some plotted data in the grid layout manager.
Write a callback function named
listBoxValueChanged
that updates the colormap for the UI axes, and assign the function to theValueChangedFcn
callback property of the list box. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.
function colormapApp fig = uifigure; g = uigridlayout(fig,[3 2]); g.RowHeight = {'1x','fit','1x'}; g.ColumnWidth = {'fit','1x'}; lb = uilistbox(g, ... "Items",["Spring","Summer","Autumn","Winter"], ... "ItemsData",{spring,summer,autumn,winter}); lb.Layout.Row = 2; lb.Layout.Column = 1; ax = uiaxes(g); ax.Layout.Row = [1 3]; ax.Layout.Column = 2; surf(ax,peaks) colormap(ax,spring) lb.ValueChangedFcn = @(src,event) listBoxValueChanged(src,event,ax); end function listBoxValueChanged(src,event,ax) cmap = event.Value; colormap(ax,cmap) end
Run the colormapApp
function. Select an item in the list box to change the colormap.
colormapApp
Add Icons to List Box Items
Since R2023a
Create a list box with three items that represent different images.
fig = uifigure; lb = uilistbox(fig,"Items",["Peppers","Nebula","Street"]);
Create three styles with icons that correspond to the list box items.
s1 = uistyle("Icon","peppers.png"); s2 = uistyle("Icon","ngc6543a.jpg"); s3 = uistyle("Icon","street1.jpg");
Add the styles to the list box items to display the icons.
addStyle(lb,s1,"item",1); addStyle(lb,s2,"item",2); addStyle(lb,s3,"item",3);
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.
Example: uilistbox(Items=["Model 1","Model 2","Model 3"])
specifies the list box items.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: uilistbox("Items",["Model 1","Model 2","Model 3"])
specifies the list box items.
Note
The properties listed here are a subset of the available properties. For the
full list, see ListBox
.
Value
— Value
element of Items
| element of ItemsData
| {}
Value, specified as an element of the Items
array,
ItemsData
array, or an empty cell array. By default,
Value
is the first element in
Items
.
To specify no selection, set Value
to an empty cell array.
Specifying Value
as an element of Items
selects the list item that matches that element. If ItemsData
is
not empty, then Value
must be set to an element of
ItemsData
, and the list box will select the associated item in
the list.
Items
— List box items
{'Item 1','Item 2', 'Item 3', 'Item 4'}
(default) | 1-by-n cell array of character vectors | string array | ...
List box items, specified as a cell array of character vectors, string array, or 1-D
categorical array. Duplicate elements are allowed. The list box displays as many options
as there are elements in the Items
array. If you specify this
property as a categorical array, MATLAB uses the values in the array, not the full set of categories.
ItemsData
— Data associated with each element of the Items
property value
empty array ([]
) (default) | 1-by-n numeric array | 1-by-n cell array
Data associated with each element of the Items
property
value, specified as a 1-by-n numeric array or a 1-by-n cell array.
Duplicate elements are allowed.
For example, if you set the Items
value
to employee names, you might set the ItemsData
value
to corresponding employee ID numbers. The ItemsData
value
is not visible to the app user.
If the number of array elements in the ItemsData
value
and the Items
value do not match, one of the
following occurs:
When the
ItemsData
value is empty, then all the elements of theItems
value are presented to the app user.When the
ItemsData
value has more elements than theItems
value, then all the elements of theItems
value are presented to the app user. MATLAB ignores the extraItemsData
elements.When the
ItemsData
value is not empty, but has fewer elements than theItems
value, the only elements of theItems
value presented to the app user are those that have a corresponding element in theItemsData
value.
Example: {'One','Two','Three'}
Example: [10 20 30 40]
Multiselect
— Multiple item selection
'off'
(default) | on/off logical value
Multiple item selection, specified as 'off'
or
'on'
, 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
.
Set this property to 'on'
to allow users to select multiple items
simultaneously.
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 function executes when the user selects a different item in the list
box. It does not execute if the Value
property setting changes
programmatically.
This callback function can access specific information about the user’s interaction
with the list box. 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 list box.
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 list box after the app user’s most recent interaction |
PreviousValue | Value of list box before the app user’s most recent interaction |
ValueIndex | Index of list box value in items after the app user’s most recent interaction |
PreviousValueIndex | Index of list box value in items before the app user’s most recent interaction |
Source | Component that executes the callback |
EventName | 'ValueChanged' |
For more information about writing callbacks, see Callbacks in App Designer.
Position
— Location and size of list box
[100 100 100 74]
(default) | [left bottom width height]
Location and size of the list box 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 list box |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the list box |
width | Distance between the right and left outer edges of the list box |
height | Distance between the top and bottom outer edges of the list box |
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 200]
Tips
Use the scroll
function to programmatically scroll a list box
item or the top or bottom of the list into view.
Version History
Introduced in R2016aR2023b: Access index of component value in list of items
Access the index of the component value in the list of items by using the
ValueIndex
property.
R2023a: Style list box items
Create styles for list box components using the uistyle
function, and add the styles to individual items or entire list box components using
the addStyle
function.
R2022b: Program a response to a user clicking or double-clicking the list box
Use the ClickedFcn
and DoubleClickedFcn
callback properties to program a response to a user clicking and double-clicking the
list box.
For more information, see ListBox
.
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 (한국어)