Drop Down Button (App Designer) return index of repeated elements

조회 수: 11 (최근 30일)
Hidd_1
Hidd_1 2022년 1월 12일
답변: Jon 2022년 1월 12일
I would like to select an option on the drop down button, the problem is that the options are reapeated!
Thus I would like that the drop down button return the option + the index of the option(S) that correspond to the chosen option.
For instance:
if the Appliance_Manufacturer ={'x','x','x','x','x','y,'z'} (Cell Array)
I would like to make the items list look like this:
{'x','y','z'}
than If I choosed the x, I wanna get:
Select_Manufacturer = [1,2,3,4,5]
which are the indexes where x is.
This is my primitive code:
function ApplianceManufacturerDropDownValueChanged(app, event)
A = app.Appliance_Manufacturer{:};
B = cellfun(@(v)sort(char(v)),A,'uni',0);
C = cellfun(@double,unique(B),'uni',0);
app.ApplianceManufacturerDropDown.Items = {app.Appliance_Manufacturer{:}};
app.ApplianceManufacturerDropDown.ItemsData = 1:length(app.ApplianceManufacturerDropDown.Items);
Select_Manufacturer = app.ApplianceManufacturerDropDown.Value;
end
I have tried to play with the unique function but I didn't get any good results.
I would appreciate any help, and thanks in advance!

채택된 답변

Jon
Jon 2022년 1월 12일
Here's a skeleton of the logic regarding using unique. You will have to adapt details regarding getting the users selection etc. Here I just illustrate for when the user selects x as you describe in your question
Appliance_Manufacturer ={'x','x','x','x','x','y','z'}
[item_list,~,ic] = unique(Appliance_Manufacturer)
idx = strmatch('x',item_list)
Select_Manufacturer = find(ic==idx)

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by