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

조회 수: 2 (최근 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

답변 (1개)

Abhinav Aravindan
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 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!

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by