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.
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.
更多回答(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!