How to show one value from two popup menu strings

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

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

No, I offer an example
Jhon has Audi Jhon has Skoda Mary has Skoda
I want to see in text who has Audi an Skoda, that's why I made two pop-up
It's just an example, for me understanding the codes. It will not be confusing. Thanks!
If you put my code in the callback for each popup, it will fill up the edit text box with the name of the users who have the car type you last clicked on. For example if you select Audi, the text box will list Maria and Jhon. If you select the Skoda popup, the text box will show Maria, Jhon, and Nick. In either case, the popups will continue to say Audi and Skoda since you have only those names, and only one name, in each popup, but the edit textbox will change to show the owners.
Thank you very much Image Analyst !
function popupmenu1_Callback(hObject, eventdata, handles)
AUDI = {'Jhon', 'Maria'};
SKODA = {'Jhon', 'Maria', 'Nick'};
MERCEDES = {'Nick'};
% Get car model
selectedCar = handles.popupmenu1.Value;
% Fill owners popup with corresponding list of owners for that model of car.
switch selectedCar
case 1
owners = AUDI;
case 2
owners = SKODA;
case 3
owners = MERCEDES;
end
% Fill owners popup with Audi owners.
str = [];
for k = 1 : length(owners)
str = fprintf('%s\n%s', str, owners{k});
end
handles.edit1.Max = 2; % I think you need this for it to allow a multi-line edit text box.
handles.edit1.String = owners;
function popupmenu2_Callback(hObject, eventdata, handles)
AUDI = {'Jhon', 'Maria'};
SKODA = {'Jhon', 'Maria', 'Nick'};
MERCEDES = {'Nick'};
% Get car model
selectedCar = handles.popupmenu2.Value;
% Fill owners popup with corresponding list of owners for that model of car.
switch selectedCar
case 1
owners = AUDI;
case 2
owners = SKODA;
case 3
owners = MERCEDES;
end
% Fill owners popup with Audi owners.
str = [];
for k = 1 : length(owners)
str = fprintf('%s\n%s', str, owners{k});
end
handles.edit1.Max = 2; % I think you need this for it to allow a multi-line edit text box.
handles.edit1.String = owners;
Hi Image Analyst, I have a new question for you. I put upper the your modified code:
What if ,when I select AUDI and MERCEDES, I want it to show me only if the owners who own both types of cars, if not, to show me that there are no owners who own both types of cars? Thank you!
Then you'll need to have a blank entry in each of the popups for the "None selected" option. Then check the value of the popup. If it's 1, non selected, if it's 2 Audi is selected, and if it's 3 Skoda is selected. Then in each callback call a function called something like "UpdateEditTextBox" where in there you check the value of the popups and create the desired string.
I removed the first value of popup
I try like this but no desired effect
newValue = [selectedCar '1'];
set(handles.edit1,'String',newValue);
AUDI = {'Jhon', 'Maria'};
SKODA = {'Jhon', 'Maria', 'Nick'};
MERCEDES = {'Nick'};
% Get car model
selectedCar = handles.popupmenu2.Value;
newValue = [selectedCar '1'];
set(handles.edit1,'String',newValue);
% Fill owners popup with corresponding list of owners for that model of car.
switch selectedCar
case 2
owners = AUDI;
case 3
owners = SKODA;
case 4
owners = MERCEDES;
end
% Fill owners popup with Audi owners.
str = [];
for k = 1 : length(owners)
str = fprintf('%s\n%s', str, owners{k});
end
handles.edit1.Max = 2; % I think you need this for it to allow a multi-line edit text box.
handles.edit1.String = owners;
I guess is something with ismember
Nick ismember of Skoda and Mercedes
Maria and Jhon ismember of Skoda and Audi
But...I dont know how to apply this one...
The popup;s must be corellated, if no match no answer, i want to make my self clear with my problem, if don't please tell me to rephrase it. Thank you
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 ?
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)

Categories

Products

Release

R2015a

Community Treasure Hunt

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

Start Hunting!