image thumbnail

Auto-Filter Listbox

version (6.35 KB) by Todd Baxter
Create listbox uicontrol where strings can be filtered with a regular expression.


Updated 25 Jul 2021

View License

H = listbox_autofilter()
H = listbox_autofilter('PropertyName', Value, ...)
H = listbox_autofilter(PARENT)
H = listbox_autofilter(PARENT, 'PropertyName', Value, ...)
Any combination of arguments that is valid for the construction of a
listbox uicontrol object. See 'uicontrol' and 'Uicontrol Properties'
documentation for more details.
H <1x1 uicontrol handle>
- Handle to listbox uicontrol object
listbox_autofilter effectively serves as a wrapper for a standard
listbox uicontrol object. See the 'uicontrol' documentation for more
information on constructing, setting/getting properties, and
interacting with a listbox uicontrol object.
Note, the 'Style' property will automatically be set to 'listbox' and
will override any other 'Style' property that may have been specified.
The 'Style' property should not be modified. Also, location and size
properties should not be modified because they are being automatically
handled through the use of the GUI Layout Toolbox.
The additional functionality provided by listbox_autofilter is the
pairing of the listbox uicontrol with an editable textbox that will
filter the listbox strings that are displayed through (case insensitive)
regular expression search.
Listbox selections will always provide an index relative to the full
set of strings, regardless of whether or not only a filtered subset of
the strings is being displayed. This is done to make it easier to use
the listbox selection to index into other variables that correspond to
the items in the listbox. See examples below for more details.
All listbox uicontrol callbacks are fully supported. The only potential
issue (for HG2) is that all callback event data will be 'struct' class.
So, the callbacks that are set shouldn't depend on receiving a specific
class of event data.
f = figure;
str1 = {'This','is','example','text','to','display','in','listbox'};
h = listbox_autofilter(f, 'String', str1);
% If 'x' is typed into the textbox, then the only strings that will be
% displayed are {'example','text','listbox'}. Retrieving the index through
get(h, 'Value')
% will either return 3, 4, or 8 depending on the selection made, because
% those are the "absolute" indices of those strings in the full set of strings.
% Most listbox uicontrol properties can be modified as normal.
str2 = {'Attention','this','is','a','test','TESTING 1','TESTING 2','TESTING 3'};
set(h, 'String', str2);
% No matter how the listbox strings are filtered, the same 'str1' string
% will be displayed when its corresponding 'str2' string is selected.
set(h, 'Callback', @(src,evnt) disp(str1{get(src,'Value')}));
% Cleanup is automatically handled when listbox uicontrol object is deleted.
GUI Layout Toolbox
HG1 version (R2014a and earlier):
HG2 version (R2014b and later):

Cite As

Todd Baxter (2022). Auto-Filter Listbox (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!