How to show one value from two popup menu strings

1 view (last 30 days)
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!

Accepted Answer

Image Analyst
Image Analyst on 13 May 2022
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 Comments
Cristian Martin
Cristian Martin on 15 May 2022
I will try other way
with a third text box
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
string1 = get(handles.edit1, 'String');
string2 = get(handles.edit2, 'String');
if strcmp(string1, string2);
handles.edit3.Max = 3;
handles.edit3.String = string1;
else
handles.edit3.Max = 3;
handles.edit3.String = 'No match';
end
is there a way to compare those two edit boxes and in the third one to show only what match ?
Cristian Martin
Cristian Martin on 15 May 2022
It's done
I let the solve here for others
function pushbutton1_Callback(hObject, eventdata, handles)
x = get(handles.edit1, 'String');
y = get(handles.edit2, 'String');
WordAB = intersect(x, y);
handles.edit3.Max = 3;
handles.edit3.String = WordAB;
qq = isempty(WordAB);
if qq == 1;
handles.edit3.String = 'Nici o potrivire';
end
Thank you Image Analyst for your support

Sign in to comment.

More Answers (0)

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!