How to show one value from two popup menu strings
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I want to help me in understanding a situation.
Let's say I have two popup menus with same values (Audi, Mercedes, Skoda) and an edittext box.
We have also 3 person (Maria, Jhon, Nick) who own one or two different cars
If I select one value (Audi) from the popup menu I want in edittext to show the owners (Maria and Jhon)
Also if I select a value (Audi) from the first popup menu and a value (Skoda) from the second popup menu I want in edittext to show who own the cars (Maria, Jhon and Nick)
I know how to extract the values from the pop up menus but I can't figure how to correlate them with variables.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/997545/image.jpeg)
all_choices = get(hObject, 'String');
selected = get(hObject, 'Value');
chosen = all_choices{selected};
set(handles.edit1, 'String',chosen);
Thank you guys!
0 件のコメント
採用された回答
Image Analyst
2022 年 5 月 13 日
Try this. Assume Audi is the first entry in the car popup, which is on the left, and the popup on the right is for owners. So then do
audiOwners = {'Maria', 'Jhon'};
skodaOwners = {'Maria', 'Jhon', 'Nick'};
% Get car model
selectedCar = handles.popCar.Value;
% Fill owners popup with corresponding list of owners for that model of car.
if selectedCar == 1
% Fill owners popup with Audi owners.
handles.popOwners.String = audiOwners;
else
% Fill owners popup with Skoda owners.
handles.popOwners.String = skodaOwners;
end
If you really want the names in the edit text box instead of a drop down list, then have a loop
audiOwners = {'Maria', 'Jhon'};
skodaOwners = {'Maria', 'Jhon', 'Nick'};
% Get car model
selectedCar = handles.popCar.Value;
% Fill owners popup with corresponding list of owners for that model of car.
if selectedCar == 1
owners = audiOwners;
else
owners = skodaOwners;
end
% Fill owners popup with Audi owners.
str = [];
for k = 1 : length(owners)
str = fprintf('%s\n%s', str, owners{k});
end
handles.edtOwners.Max = 2; % I think you need this for it to allow a multi-line edit text box.
handles.edtOwners.String = str;
But I think it would be confusing for the user to have two dropdown lists, each with the name of only one kind of car.
10 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!