Get value from popup menu and use it in different function

Hi; I am doing GUI which has a popup menu that allow the user to choose from several options, lets say I have three options each one has different values for (I,V,P) current, voltage and power. my coding is like this (for example):
function popupmenu1_Callback(hObject, eventdata, handles)
switch get(handles.popupmenu,'Value')
case 1
I=7;
V=20;
p=130;
case 2
I=5;
V=30;
p=170;
otherwise
end
then I need to use these values in some calculation in pushbutton_callback, which it coding as below(for example):
function pushbutton1_Callback(hObject, eventdata, handles)
what code can I use to get these values from the popup menu ?? I need your help please.

1 comentario

i am tring to use the value of first popupmenu1 in another popupmenu2 but the problem is,it is not updating the value of S (7 20 130). always showing S (5 30 170) how can we use the value of popupmenu1 case 1 i.e I=7; V=20;p=130; in another popupmenu2 case 1,case 2 ... and generate the result and the same case for popmenu1 case 2 value i.e I=5; V=30;p=170; in another popupmenu2 case 1,case 2...and so on and generate the result

Iniciar sesión para comentar.

Respuestas (4)

Jan
Jan el 15 de Mayo de 2012
Either store the values in the popup's UserData:
function popupmenu1_Callback(hObject, eventdata, handles)
switch get(handles.popupmenu,'Value')
case 1
S.I=7;
S.V=20;
S.p=130;
case 2
S.I=5;
S.V=30;
S.p=170;
otherwise
end
set(handles.popupmenu, 'UserData', S);
Then you can access the values by:
S = get(handles.popupmenu, 'UserData');
Or use the handles struct:
function popupmenu1_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Update!
switch get(handles.popupmenu,'Value')
case 1
S.I=7;
S.V=20;
S.p=130;
case 2
S.I=5;
S.V=30;
S.p=170;
otherwise
end
handles.S = S;
guidata(hObject, handles); % Update!

8 comentarios

I Can't get any Values in call back function.
shall you explain how can i write the code in push button function ?
I used this way:
S = get(handles.popupmenu, 'UserData');
I=S.I;
V=S.V;
P=S>P;
but its not working.. please help.
Jan
Jan el 16 de Mayo de 2012
Please explain what "it's not working" means. Do you get an error message? Is the "handles" struct delivered in the inputs of the push-button callback uptodate - most of all, does it contain the field ".popupmenu"? If not, use GUIDATA for updating as shown in the examples I have posted.
I am getting an error when pressing the push button, saying that :
??? Error using ==> mrdivide
Matrix dimensions must agree.
??? Error while evaluating uicontrol Callback
as I can see, I don't think the handles is delivered in the input of push-button.
I think you should do it in this way at your push button:
S = get(handles.popupmenu1,'Value'); %get currently selected option from menu
S = handles.S; % import
and at the end of your code in the pop up menu add this:
handles.S = S;
I hope it help you...
Walter Roberson
Walter Roberson el 23 de Abr. de 2013
Editada: Walter Roberson el 23 de Abr. de 2013
That would fetch the Value from the control, and then overwrite that fetched value with the previously stored information. You should do one or the other, not both.
Samer did not show any division in the code, so we cannot say what was causing the mrdivide problem.
I want to ask that how can we use the values of handles.S in push button ...if iam using If else command in push button to either select 'Case 1' of popup menu or 'case 2' so how can i select the cases of Popmenu inside a pushbutton ..can anyone share code with me .Thanks
Thanks Jam Simon, 1st method worked for me, but to use the variables of popupmenu_callback (i.e S.V, S.P, S.I) in pushbutton_callback I had to use the following commands as well:
V=S.V;
P=S.P;
I=S.I;
but thanks a lot for your answer.
If I write
V=S.V;
P=S.P;
I=S.I;
type of code. I get an error 'Dot indexing is not supported for variables of this type.'
Please help me

Iniciar sesión para comentar.

sourabh jain
sourabh jain el 6 de Jun. de 2015
Editada: Walter Roberson el 6 de Jun. de 2015
i am tring to use the value of first popupmenu1 in another popupmenu2 but the problem is,it is not updating the value of S (7 20 130). always showing S (5 30 170)
see the code
function popupmenu1_Callback(hObject, eventdata, handles)
switch get(handles.popupmenu,'Value')
case 1
S.I=7;
S.V=20;
S.p=130;
case 2
S.I=5;
S.V=30;
S.p=170;
otherwise
end
set(handles.popupmenu, 'UserData', S);
function popupmenu2_Callback(hObject, eventdata, handles)
switch get(handles.popupmenu,'Value')
case 1
set(handles.popupmenu, 'UserData', S);
i=S.I
v=S.V
p=i*v
case 2
set(handles.popupmenu, 'UserData', S);
i=S.I
v=S.V
p=i*v
otherwise
end

2 comentarios

Then quit referring to "handles.popupmenu" - use either handles.popupmenu1 or handles.popupmenu2, whichever you want.
KALYAN KUMAR
KALYAN KUMAR el 1 de Jul. de 2015
Editada: KALYAN KUMAR el 1 de Jul. de 2015
can you help me on this issue
function popupmenu8_Callback(hObject, eventdata, handles) handles = guidata(hObject); contents = get(handles.popupmenu8,'Value') switch contents case 1
S.M=7;
S.P=20;
case 2
S.M=5;
S.P=30;
otherwise
end
set(handles.popupmenu8, 'UserData', S);
guidata(hObject, handles);

Iniciar sesión para comentar.

In the second popup you need
S = get(handles.popupmenu, 'UserData')

1 comentario

can u please resolve this issue for me as i have tried the above code i am getting error function popupmenu8_Callback(hObject, eventdata, handles) % hObject handle to popupmenu8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles = guidata(hObject); % Update! contents = get(handles.popupmenu8,'Value') switch contents case 1
S.M=7;
S.P=20;
case 2
S.M=5;
S.P=30;
otherwise
end
%handles.S = S; set(handles.popupmenu8, 'UserData', S); guidata(hObject, handles);

Iniciar sesión para comentar.

Deepa AS
Deepa AS el 29 de Jul. de 2015
How should the variable declared in popup menu be called in pushbutton call back function . Can anybody help me out with this? Please.
Thank you Deepa

1 comentario

Jan
Jan el 29 de Jul. de 2015
Please open a new thread for a new question. This is the section for answers.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Environment Customization en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Mayo de 2012

Comentada:

el 16 de En. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by