Filter löschen
Filter löschen

How can I pass variables (declare in a pop up menu) in a pushbotton section of GUI ?

4 Ansichten (letzte 30 Tage)
Hi, I am doing GUI which has a popup menu that allow the user to choose from several options (different type of steel), lets say I have three options each one has different values (Drho0, Ntot and Scalamobi). My coding is like this (for example):
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
switch get(handles.popupmenu1,'Value')
case 304
p1 = -3.929e+12;
p2 = 9.107e+12;
p3 = -7.5e+12;
p4 = 2.771e+12;
p5 = -2.709e+11;
S.Drho0=(p1*riduzione^4+p2*riduzione^3+p3*riduzione^2+p4*riduzione+p5);
S.Ntot=1.311e-69*Drho0^(6.975);
S.Scalamobi=1e-04; %%coefficiente di riscalamento che tiene conto della presenza di leganti
case 316
S.Drho0 = (2.8e11*(e)+1.57e10);
S.Ntot = (-5.23e8*(e)^2+ 1.76e9*(e) - 1.318e8);
S.Scalamobi=1.2e-05;
otherwise
end
set(handles.popupmenu, 'UserData', S);
I tried to use an old answer from matlabcentral: https://it.mathworks.com/matlabcentral/answers/38419-get-value-from-popup-menu-and-use-it-in-different-function. But I am getting an error when I try to access my variable in the push- button callback :
S = get(handles.popupmenu1, 'UserData');
Drho0=S.Drho0;
Ntot=S.Ntot;
Scalamobi=S.Scalamobi;
I am getting this error: "Struct contents reference from a non-struct array object". What am I doing wrong ? Thanks in advance
  2 Kommentare
Adam
Adam am 15 Mai 2017
Which line throws the error? You can use
dbstop if error
to force the code to stop on the error line (or use the Breakpoints menu).
Giuseppe Napoli
Giuseppe Napoli am 15 Mai 2017
Error in switchprova>pushbutton1_Callback (line 281) Drho0=S.Drho0;
I think that Matlab does not recognize the S. variable

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Adam
Adam am 15 Mai 2017
I didn't notice this before. Maybe it is a typo in your question, but I will assume you just pasted in from your code:
set(handles.popupmenu, 'UserData', S);
...
S = get(handles.popupmenu1, 'UserData');
You are missing the '1' off the end of handles.popupmenu. I'm surprised this line does not cause an error though as I wouldn't have though it was valid syntax if you don't also have a graphics object called handles.popupmenu.
  2 Kommentare
Giuseppe Napoli
Giuseppe Napoli am 15 Mai 2017
I pasted from my code and I forgot to put the 1 at the end of popupmenu. But I am getting the same error, in the same line :
S = get(handles.popupmenu1, 'UserData');
Drho0=S.Drho0; % LINE 284
Ntot=S.Ntot;
Struct contents reference from a non-struct array object.
Error in switchprova>pushbutton1_Callback (line 284) Drho0=S.Drho0;
Adam
Adam am 15 Mai 2017
I would recommend just using standard handle usage rather than attaching to the 'UserData' of a UI component - i.e.
handles.S = S;
guidata( hObject, handles );
in your popup callback, then
S = handles.S;
in your pushbutton callback, or just use it directly as handles.S if you prefer. I would name it something more meaningful personally too, but since you called it S I did the same.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by