How do i add search functionality into my drop down menu in my GUI?
5 views (last 30 days)
Show older comments
I have a GUI that loads data and plots it. I have drop down menus for selecting the parameters to plot. I wanted to add search functionality to the drop down menu. Any suggestions?
2 Comments
Rishabh Rathore
on 31 May 2018
Can you elaborate as to search what? Search from existing values for drop down?
Answers (2)
Arsalan jamialahmadi
on 13 Feb 2019
You can add en uieditfield called MyEditField to your app and for that apply a "ValueChanging" callback to be able to search your MyDropDown:
changingValue = event.Value;
List=[];
if ~isempty(app.MyEditField)
for i=1:length(app.OriginalList)
if contains(app.OriginalList{i},changingValue)
List=[List,app.OriginalList(i)];
end
end
end
if isempty(app.MyEditField)
List=app.OriginalList;
end
if ~isempty(List)
app.MyDropDown.Items=List;
end
if isempty(event.Value)
app.MyDropDown.Items=app.OriginalList;
end
if ~isempty(event.Value) && isempty(List)
app.MyDropDown.Items={};
end
0 Comments
Bereketab Gulai
on 25 May 2020
Here is much Modified of Arsalan jamialahmadi
% Value changing function: TestTypeSearchEditField
function TestTypeSearchEditFieldValueChanging(app, event)
persistent originalTestTypeList; % save the original list
if isempty(originalTestTypeList)
originalTestTypeList = app.TestTypeDropDown.Items;
pause(0.5); % sync value (in case...)
end
changingValue = event.Value;
Utility.filterDropdownList(app.TestTypeDropDown, originalTestTypeList, changingValue);
end
In Utility Class (You can create this)
function filterDropdownList(uidropdownControl, originalList, changingValue)
List=[];
if ~isempty(changingValue)
for c = 1:length(originalList)
if contains(originalList{c},changingValue, "IgnoreCase",true)
List = [List,originalList(c)];
end
end
end
if ~isempty(List) % Something is found
uidropdownControl.Items = List;
elseif ~isempty(changingValue) && isempty(List) % Nothing is found
uidropdownControl.Items = {};
else % Restore otherwise
uidropdownControl.Items = originalList;
if isempty(uidropdownControl.ItemsData)
uidropdownControl.Value = originalList{1};
else
if isnumeric(uidropdownControl.ItemsData)
uidropdownControl.Value = uidropdownControl.ItemsData(1);
else
uidropdownControl.Value = uidropdownControl.ItemsData{1};
end
end
end
end
0 Comments
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!