How can I pass variables (declare in a pop up menu) in a pushbotton section of GUI ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Adam
2017-5-15
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).
回答(1 个)
Adam
2017-5-15
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 个评论
Adam
2017-5-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!