Why won't my GUI code work!?

4 次查看(过去 30 天)
Grant Dickson
Grant Dickson 2012-11-21
I have built a GUI using GUIDE. I have a lot of handles so have only included the code of the call back functions for one of each handle type here: c_callback (edit text box), c_slider_callback(slider), c_unit_callback(drop down menu), and calculate.callback(push button):
-------------------------------------------------------------------------------
function c_Callback(hObject, eventdata, handles)
% Obtain the new c value entered in the box
NewVal = str2double(get(hObject,'String'));
% If there is no value or it is outwith limits, restore to the previous value from the slider
if isempty(NewVal) || (NewVal<get(handles.c_slider,'Min')) || (NewVal>get(handles.c_slider,'Max'))
set(hObject,'String',get(handles.c_slider,'Value'));
% Otherwise the value is correct so change the slider value
else
set(handles.c_slider,'Value',NewVal)
end
-------------------------------------------------------------------------------
% --- Executes on button press in calculate.
function Calculate_Callback(hObject, eventdata, handles)
c_unit_Callback(hObject, eventdata, handles)
h_0_unit_Callback(hObject, eventdata, handles)
omega_unit_Callback(hObject, eventdata, handles)
r_0_unit_Callback(hObject, eventdata, handles)
F_unit_Callback(hObject, eventdata, handles)
n_p = (get(handles.n_p_slider, 'value'));
c = (get(handles.c_slider, 'value'))*c_unit;
nu = (get(handles.nu_slider, 'value'));
h_0 = (get(handles.h_0_slider, 'value'))*h_0_unit;
omega = (get(handles.omega_slider,'value'))*omega_unit;
r_0 = (get(handles.r_0_slider, 'value'))*r_0_unit;
F = (get(handles.F_slider, 'value'))*F_unit;
n = (get(handles.n_slider, 'value'));
% THIS IS JUST CODE FOR CALCULATIONS. NOT IMPORTANT:
theta = (0.8*360/n_p)*pi/180;
r = r_0;
F_max = 0;
while F_max < F;
r = r + 10E-3; % increase radius by 1 mm
U = r*omega; % calculate tangential velocity U at radius r
L = r*theta; % calculate arc length L at radius r
x = linspace(0,L,n+1);
delta_x = L/n;
h = h_0 + x*tan(c);
p = zeros(1,n+1);
p_max_old = 0; p_max_new = 1;
while abs(p_max_new-p_max_old)>(0.001E-2*p_max_new)
p_max_old=p_max_new;
for i=2:n
p(1,(i)) = (p(1,(i+1)) + p(1,(i-1)))/2 ...
+ (3*c*(p(1,(i+1)) - p(1,(i-1))) * delta_x)./(4*h(1,i))...
+ (3*nu*U*c*delta_x^2)./h(1,i).^3;
end
p_max_new=max(p);
end
p_mean = mean(p);
A = pi*(r^2-r_0^2)*theta/(2*pi); % find area of pad
F = p_mean*A; % find maximum force
end
% END OF CALCULATIONS. IMPORTANT:
% display the results in the text boxes
set(handles.P_max,'string',num2str(p_max_new));
set(handles.A,'string',num2str(A));
set(handles.D,'string',num2str(2*r));
-------------------------------------------------------------------------------
% --- Executes on selection change in c_unit.
function c_unit_Callback(hObject, eventdata, handles)
% retrieve unit selection from drop down list
str = get(hObject, 'String'); % Obtain the pop up selection string
val = get(hObject, 'Value'); % Obtain the pop up selection value
switch str{val};
case 'mrad' % If the choice is mrad set c_unit to 1E-3
c_unit=1E-3;
case 'rad' % If the choice is rad set c_unit to 1
c_unit=1;
case 'deg' % If the choice is rad set c_unit to pi/180
c_unit=pi/180;
end
-------------------------------------------------------------------------------
% --- Executes on slider movement.
function c_slider_Callback(hObject, eventdata, handles)
% Put the current slider value in the corresponding text box
set(handles.c,'String',num2str(get(hObject,'Value')));
-------------------------------------------------------------------------------
When I run the code the GUI appears. When I click the calculate button the following error appears:
-------------------------------------------------------------------------------
??? Cell contents reference from a non-cell array object.
Error in ==> GUI>c_unit_Callback at 454
switch str{val};
Error in ==> GUI>Calculate_Callback at 305
c_unit_Callback(hObject, eventdata, handles)
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)GUI('Calculate_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
-------------------------------------------------------------------------------
Line 454 is: switch str{val};
Any help is much appreciated. Thanks

回答(3 个)

Matt Fig
Matt Fig 2012-11-21
What is in the popup menu? To see this, take the semicolon off of the line where you retrieve the string, like this:
str = get(hObject, 'String')% Obtain the pop up selection string
Then when you run the GUI and select something from that popup, show us what prints out to the screen.
  1 个评论
Matt Fig
Matt Fig 2012-11-22
Grand comments:
Thanks for the reply. I defined the popup menu with the string field in the property editor of GUIDE. Following your instructions, the command window shows str =
'mrad'
'rad'
'deg'
Hopefully this helps you work out my problem

请先登录,再进行评论。


Jan
Jan 2012-11-21
编辑:Jan 2012-11-21
Please set a breakpoint in the line 454 "switch str{val}". Then run the program until it stops there. Now check the type and size of "str".
Another method to debug is:
dbstop if error
Then the program stops, if the error occurs. Then the variable "str" can be checked again. I guess, that the callback function is called from another function also, which replies a string instead of a cell string.
  1 个评论
Matt Fig
Matt Fig 2012-11-22
Grant comments
Okay,
setting the breakpoint and running the program to there, str is: >> str ??? Undefined function or variable 'str'.
Then typing dbstop if error into the command window and checking str:
K>> str
str =
Calculate
str shouldn't be Calculate, it should be: 'mrad' 'rad' 'deg'
Any idea where the problem lies? I'll search for it in the meantime

请先登录,再进行评论。


Nick
Nick 2013-1-25
by using:
str = get(hObject, 'String');
You call the name/ tag of the popupmenu and not the string that is inside the popupmenu. From the comment you gave on Jan Simon I gess the Tag you assigned to your popupmenu is Calculate. If this is true I would surgest the following code:
handles = guidata(gcbo);
str = get(handles.Calculate, 'String');
%str now contains all strings from the popupmenu
val = get(handles.Calculate,'Value');
%the value now is the selected value from the popupmenu
switch str(val)
%etc

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by