How can I get the "List of Items" in real time that "Contains" the "Entry String" in UI DropDown Component?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Abhinav Aravindan
2024-10-14
编辑:Abhinav Aravindan
2024-10-14
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 “ValueChangedFcn”callback 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!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!