MATLAB: Get multiple values from popup menu and use it in different function

guimultiplepopupselectionstringvalue

Hi, In the GUI I'm working, it has a pop-up menu with more than 20 options, for example person1, person2, person3…. The user can select more than one option. To allow the user to select multiple options, I defined max and min properties as 2 and 0. But, I would like to know how to get the values of multiple selections from pop-up menu to use them in a different function. Here is my code for your reference.
function popupmenu1_Callback(hObject, eventdata, handles)
str = get(handles.popupmenu,'string');
val = get(handles.popupmenu,'value');
switch str{val};
case 'person1'
name = x;
age = 26;
case 'person2'
name = y;
age = 24;
case 'person3'
name = z;
age = 23;
end
Here, in the variable 'str', i get only the string of first selection in popupmenu. I want to know how to get how many options the user has selected and how to get the values of multiple selections.

Best Answer

If multiple values are selected, value is a vector and not a scalar. Therefore you can use:
str = get(handles.popupmenu,'string');
val = get(handles.popupmenu,'value');
for iVal = 1:length(val)
aVal = value(iVal)
switch str{aVal};
case 'person1'
name = x;
age = 26;
case 'person2'
name = y;
age = 24;
case 'person3'
name = z;
age = 23;
end
end