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

3 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 8 Feb 2015
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
  1 Comment
rgb RK
rgb RK on 20 Feb 2015
Thank you so much for your answer.But I got the error
Undefined function or method 'value' for input arguments of type 'double'.
Error in ==> gui_function at 7
aVal = value(iVal);
??? Error while evaluating uicontrol Callback
I tried to find the solution from different other questions. Still I am unable to solve. Could you please help me how to fix this issue.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!