How do i add search functionality into my drop down menu in my GUI?

5 ビュー (過去 30 日間)
Anuj
Anuj 2018 年 5 月 30 日
回答済み: Bereketab Gulai 2020 年 5 月 25 日
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 件のコメント
Rishabh Rathore
Rishabh Rathore 2018 年 5 月 31 日
Can you elaborate as to search what? Search from existing values for drop down?
Anuj
Anuj 2018 年 7 月 11 日
Yes. Search for already loaded data.

サインインしてコメントする。

回答 (2 件)

Arsalan jamialahmadi
Arsalan jamialahmadi 2019 年 2 月 13 日
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

Bereketab Gulai
Bereketab Gulai 2020 年 5 月 25 日
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

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by