File Exchange

image thumbnail

Auto-Filter Listbox

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

3 Downloads

Updated 21 Jun 2020

View License

Usage:

H = listboxAutoFilter()
H = listboxAutoFilter('PropertyName', Value, ...)
H = listboxAutoFilter(PARENT)
H = listboxAutoFilter(PARENT, 'PropertyName', Value, ...)

Inputs:

Any combination of arguments that is valid for the construction of a
listbox uicontrol object. See 'uicontrol' and 'Uicontrol Properties'
documentation for more details.

Outputs:

H <1x1 uicontrol handle>
- Handle to listbox uicontrol object

Description:

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

Examples:

f = figure;
str1 = {'This','is','example','text','to','display','in','listbox'};
h = listboxAutoFilter(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.
delete(h);

Requires:

GUI Layout Toolbox
HG1 version (R2014a and earlier): https://www.mathworks.com/matlabcentral/fileexchange/27758-gui-layout-toolbox
HG2 version (R2014b and later): https://www.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox

References:

http://undocumentedmatlab.com/articles/auto-completion-widget

Cite As

Todd Baxter (2020). Auto-Filter Listbox (https://www.mathworks.com/matlabcentral/fileexchange/74790-auto-filter-listbox), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

Updates

1.0.1

Updated formatting and function naming convention

1.0.0.1

Uploaded an appropriately named .zip file containing the code.

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