How to find position of a number in an array using a drop down

조회 수: 7 (최근 30일)
Adrian Quesada
Adrian Quesada 2019년 5월 21일
편집: Adam Danz 2020년 8월 18일
Lets say P=[1 2 3;4 5 6; 7 8 9] is there any way to use a drop down in GUI or App Designer to search for a value in the first column and that the code returns the second and third value of the same row?.
The final array have 100 rows, so if I use cases on the drop down the code will be so big.
Thanks for your help
  댓글 수: 1
Adam Danz
Adam Danz 2019년 5월 21일
Drop down menus merely present a list that allows the user to select one or more items. Drop down menus do not "search" for anything.
It would be easy to create a drop down list of all items in the first row of the matrix. Is it your intention that the user would then select one of those items and then return the other items in that row? If that's the case, are all items in row 1 unique or will there be more than 1 row with the same number in the first column? Are your data all integers?

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2019년 5월 21일
편집: Adam Danz 2020년 8월 18일
Here's how to create a quick UI dropdown list populated with values from column 1 of your matrix. After the user makes a selection, values from columns 2 and 3 appear in the text box. To run it, copy and paste into a script and run the script.
% Your matrix data
P = reshape(1:300,3,[])';
% Create UI figure with dropdown
fig = uifigure;
fig.Position([3,4]) = [215,275];
dd = uidropdown(fig);
dd.Position = [60,220,100,22];
% Store first row of matrix in dropdown
dd.Items = strsplit(num2str(P(:,1)'));
% add a text area to display results
txa = uitextarea(fig);
txa.Position = [20,100,180,22];
txa.HorizontalAlignment = 'center';
txa.Editable = 'off';
% Assign callback function (after txa is created)
dd.ValueChangedFcn = @(dd,event)valueSelectionCallback(dd,txa,P);
% Create callback function to respond to user selection
function valueSelectionCallback(dd, txa, P)
% Return columns 2 and 3 from the row selected
v = P(P(:,1) == str2double(dd.Value), [2,3]);
% Update the text box with vector
txa.Editable = 'on';
txa.Value = sprintf('[%s]', num2str(v));
txa.Editable = 'off';
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by