How can I get the "List of Items" in real time that "Contains" the "Entry String" in UI DropDown Component?

2 views (last 30 days)

The Dropdown UI Component lists the Items only if the Start of the string matches with real time entry data. Would like to know how to get the list of all Items that contains the string that is getting typed into the Dropdown edit field?
For Example: If the List of Items contains {dharini giridhar dhavan}, when I start typing into the drop down field with 'dha', the dropdown only lists {dharini dhavan}. I would like to have {giridhar} also listed as this name also contains 'dha' string

Answers (1)

Abhinav Aravindan
Abhinav Aravindan on 14 Oct 2024
Edited: Abhinav Aravindan on 14 Oct 2024
Hi Rameshwar,
I understand that you want the uidropdown to list the options based on the substring entered in the editable field. One possible way to do this is by using a callback function to filter your options as required and a ValueChangedFcncallback property with uidropdown as follows:
function dropdown_example
% Create a figure
fig = uifigure('Position', [100 100 400 300]);
% Define the list of items
items = {'dharini', 'giridhar', 'dhavan'};
% Create a dropdown component
dd = uidropdown(fig, ...
'Position', [100 200 200 22], ...
'Items', items, ...
'Editable', 'on', ...
'ValueChangedFcn', @(src, event) filterDropdown(src, items));
end
function filterDropdown(dd, items)
% Get the current text in the dropdown
typedText = dd.Value;
% Filter items that contain the typed text
if ~isempty(typedText)
filteredItems = items(contains(items, typedText, 'IgnoreCase', true));
else
filteredItems = items;
end
% Update the dropdown items
dd.Items = filteredItems;
% If no items match, keep list empty
if isempty(filteredItems)
dd.Items = {};
end
end
Output:
Note: After typing in the editable field, you may need to press 'Enter' to update the options in the dropdown list.
Please find the relevant documentation below for your reference.
I hope this answers your query!

Categories

Find more on Modeling in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!