Fixing error while evaluating callback for GUI

1 次查看(过去 30 天)
I am trying to build a GUI for this Optimisation file Opti_final.m
%Initialisation---------------------------------------
clc;clear;format short g;format compact;
%Inputs-Part 1:Physical Problem Inputs--------------------------------------------------
P_bar=str2num(get(findobj('tag','Pressure'),'string')); %Pressure of vessel (Bar)
c=str2num(get(findobj('tag','Capacity'),'string')); %Capacity (Litres)
nd=str2num(get(findobj('tag','Safety Factor'),'string')); %Desired safety factory
L_t=str2num(get(findobj('tag','Length Constraint'),'string')); %Total length constraint of vessel (m)
H=str2num(get(findobj('tag','Height Constraint'),'string')); %Height of vessel (m)
%Inputs-Part 2:Search Parameters-------------------------------------------------------------------
material(1).Sy=170; material(1).t=[2,4,6,8,10,20,30]; material(1).CF=1.00;
material(2).Sy=280; material(2).t=[1,2,3,4,6,8,10,12,14,16,18,20]; material(2).CF=1.37;
material(3).Sy=180; material(3).t=[2,4,6,8,10,20]; material(3).CF=0.88;
material(4).Sy=300; material(4).t=[2,4,6,8,10,20]; material(4).CF=1.47;
material(5).Sy=190; material(5).t=[2,4,6,8,10,20]; material(5).CF=0.93;
material(6).Sy=320; material(6).t=[2,4,6,8,10,20]; material(6).CF=1.57;
material(7).Sy=220; material(7).t=[2,4,6,8,10,20]; material(7).CF=1.08;
material(8).Sy=370; material(8).t=[1,2,3,4,6,7,8,9,10,12,14,16]; material(8).CF=1.81;
material(9).Sy=210; material(9).t=[2,4,6,8,10,14,16,20,30,40]; material(9).CF=1.03;
material(10).Sy=390; material(10).t=[1,2,3,4,6,8,10,12,14,16,18,20]; material(10).CF=1.91;
material(11).Sy=260; material(11).t=[ 2,4,6,8,10,12,16,20,30,40,50]; material(11).CF=1.27;
material(12).Sy=440; material(12).t=[2,4,6,8,10]; material(12).CF=2.16;
material(13).Sy=270; material(13).t=[1,2,3,4,5,6,7,8,9,10]; material(13).CF=1.32;
material(14).Sy=460; material(14).t=[1,2,3,4,5,6,7,8,9,10]; material(14).CF=2.25;
material(15).Sy=290; material(15).t=[2,4,6,8,10,12,18,20,30]; material(15).CF=1.42;
material(16).Sy=490; material(16).t=[1,2,3,4,5,6,7,8,9,10]; material(16).CF=2.40;
material(17).Sy=310; material(17).t=[1,2,3,4,5,6,7,8,9,10]; material(17).CF=1.52;
material(18).Sy=530; material(18).t=[10,20,30]; material(18).CF=2.60;
material(19).Sy=340; material(19).t=[4,5,8,14,20]; material(19).CF=1.67;
material(20).Sy=580; material(20).t=[2,4,6,8,10]; material(20).CF=2.84;
material(21).Sy=370; material(21).t=[2,4,6,8,10,15,18,20]; material(21).CF=1.81;
material(22).Sy=420; material(22).t=[2,4,6,8,10,12,14]; material(22).CF=2.06;
material(23).Sy=460; material(23).t=[1,2,4,6,8,10,12]; material(23).CF=2.25;
%Inputs-Part 3: Material Variables-----------------------------------------
ris=(get(findobj('tag','ris'),'string'));
ristep=(get(findobj('tag','ristep'),'string'));
rie=H/2;
%Exhaustive Search------------------------------------------------------
P=P_bar*10e5; %converts pressure from bar to Pa
Vi=c/1000; %converts capacity from litres to m^3
sol=[];
for ri=ris:ristep:rie;
L=(Vi/(pi*ri^2))-(4*ri/3);
if L<=L_t && L>0
for i=1:length(material)
Sy=material(i).Sy*10e6;
stress_all=Sy/nd;
for j=1:length(material(i).t)
t=material(i).t(j)/1000;
ro=ri+t;
if 2*ro<=H && L+2*ro<=L_t
if t/ri<0.05
stress_t=P*ri/t;
stress_r=0;
stress_l=P*ri/(2*t);
else
stress_t=((ri^2*P)/(ro^2-ri^2))*(1+(ro^2/ri^2));
stress_r=((ri^2*P)/(ro^2-ri^2))*(1-(ro^2/ri^2));
stress_l=((ri^2*P)/(ro^2-ri^2));
end
stress_VM= sqrt(((stress_t-stress_r).^2+(stress_t-stress_l).^2+(stress_l-stress_r).^2)./2);
if stress_VM<= stress_all
V_mat= 2*pi*ri*t*L+4*pi*ri^2*t;
for k=1:length(material(i).CF)
CF=material(i).CF(k);
CI=V_mat*CF;
stress_alldisp=stress_all/10e6;
stress_VMdisp=stress_VM/10e6;
L_tDisp=L+2*ro;
Hdisp=2*ro;
sol=[sol;i,ri,t,L,stress_alldisp,stress_VMdisp,V_mat,CI,L_tDisp,Hdisp];
end
end
end
end
end
end
end
sol_sorted=sortrows(sol,8);
opt_sol=sol_sorted(1,:);
%--------------------------------------------------------------------------------
%Display the results
disp(['Optimum Material = ',num2str(opt_sol(1))])
disp(['Optimum ri (m) = ',num2str(opt_sol(2))])
disp(['Optimum thickness (m) = ',num2str(opt_sol(3))])
disp(['Optimum Lenght (m)= ',num2str(opt_sol(4))])
disp(['Allowable stress (MPa) = ',num2str(opt_sol(5))])
disp(['Von Mises Stress (MPa)= ',num2str(opt_sol(6))])
disp(['Optimum Volume of material (m^3) = ',num2str(opt_sol(7))])
disp(['Optimum Cost Index = ',num2str(opt_sol(8))])
disp(['Optimum Total Lenght (m) = ',num2str(opt_sol(9))])
disp(['Optimum Height (m) = ',num2str(opt_sol(10))])
%Output
set(findobj('tag','i'),'string',num2str(opt_sol(1)));
set(findobj('tag','ri'),'string',num2str(opt_sol(2)));
set(findobj('tag','t'),'string',num2str(opt_sol(3)));
set(findobj('tag','L'),'string',num2str(opt_sol(4)));
set(findobj('tag','stress_all'),'string',num2str(opt_sol(5)));
set(findobj('tag','stress_VM'),'string',num2str(opt_sol(6)));
set(findobj('tag','V_mat'),'string',num2str(opt_sol(7)));
set(findobj('tag','CI'),'string',num2str(opt_sol(8)));
set(findobj('tag','T_Length'),'string',num2str(opt_sol(9)));
set(findobj('tag','T_Height'),'string',num2str(opt_sol(10)));
For this i have coded a GUI file
%GUI Group project
%Main figure containing the uicontrols
my_figure = figure('Position', [100, 100, 420, 700]);
%--------------------------------------------------------------------------
%Edit boxes for design parameter inputs
%Edit box for Pressure of vessel [Pa]
uicontrol('Parent', my_figure, 'tag', 'Pressure', 'Style', 'edit', 'Position', [90, 670,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Pressure (bar) =', 'Position', [10, 660, 70, 30])
%Edit box for Capacity of vessel [litres]
uicontrol('Parent', my_figure, 'tag', 'Capacity', 'Style', 'edit', 'Position', [90, 630,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Capacity (litres)=', 'Position', [10, 625, 70, 30])
%Edit box for Safety Factor
uicontrol('Parent', my_figure, 'tag', 'Safety Factor', 'Style', 'edit', 'Position', [90, 590,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Safety Factor =', 'Position', [10, 580, 70, 30])
%Edit box for Height of vessel [m]
uicontrol('Parent', my_figure, 'tag', 'Height Constraint', 'Style', 'edit', 'Position', [90, 550,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Height Constraint (m) =', 'Position', [10, 540, 70, 40])
%Edit box for total length of vessel [m]
uicontrol('Parent', my_figure, 'tag', 'Length Constraint', 'Style', 'edit', 'Position', [90, 510,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Lenght Constraint (m) =', 'Position', [10, 500, 70, 40])
%--------------------------------------------------------------------------
%Text (Experimental)
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Values for design variables when optimised for Cost Index', 'Position', [60, 430, 300, 40])
%---------------------------------------------------------------------------------------------------------------
%Edit boxes for search parameters
%Edit box for inner radius lower limit [m]
uicontrol('Parent', my_figure, 'tag', 'ris', 'Style', 'edit', 'Position', [300, 610,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Inner radius lower (m) =', 'Position', [220, 610, 80, 30])
%Edit box for inner radius gridsize [m]
uicontrol('Parent', my_figure, 'tag', 'ristep', 'Style', 'edit', 'Position', [300, 570,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Inner radius gridsize (m) =', 'Position', [220, 570, 80, 30])
%--------------------------------------------------------------------------
%Edit boxes for outputs
%Edit box for optimal material no.
uicontrol('Parent', my_figure,'tag','i','Style','edit','Position',[160,400,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Material no. =', 'Position', [70, 390, 80, 30])
%Edit box optimal inner radius [m]
uicontrol('Parent', my_figure, 'tag','ri', 'Style', 'edit', 'Position', [160,360,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Inner radius (m) =', 'Position', [70, 350, 80, 30])
%Edit box for optimal thickness [m]
uicontrol('Parent', my_figure, 'tag', 't', 'Style', 'edit', 'Position', [160, 320,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Thickness (m) =', 'Position', [70, 310, 80, 30])
%Edit box for optimal length [m]
uicontrol('Parent', my_figure, 'tag', 'L', 'Style', 'edit', 'Position', [160, 280,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Side Length (m) =', 'Position', [70, 270, 80, 30])
%Edit box for optimal allowable stress [m]
uicontrol('Parent', my_figure, 'tag', 'stress_all', 'Style', 'edit', 'Position', [160, 240,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Allowable Stress (MPa) =', 'Position', [70, 230, 80, 30])
%Edit box for optimal Van Mises stress [m]
uicontrol('Parent', my_figure, 'tag', 'stress_VM', 'Style', 'edit', 'Position', [160, 200,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Van Mises Stress (MPa) =', 'Position', [70, 190, 80, 30])
%Edit box for optimal Volume of material [m]
uicontrol('Parent', my_figure, 'tag', 'V_mat', 'Style', 'edit', 'Position', [160, 160,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Volume of Material (m^3) =', 'Position', [70, 150, 80, 30])
%Edit box for optimal Cost Index [m]
uicontrol('Parent', my_figure, 'tag', 'CI', 'Style', 'edit', 'Position', [160, 120,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Cost Index =', 'Position', [70, 110, 80, 30])
%Edit box for total ength [m]
uicontrol('Parent', my_figure, 'tag', 'T_Length', 'Style', 'edit', 'Position', [160, 80,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Total Length (m) =', 'Position', [70, 70, 80, 30])
%Edit box for Height [m]
uicontrol('Parent', my_figure, 'tag', 'T_Height', 'Style', 'edit', 'Position', [160, 40,100,20])
uicontrol('Parent', my_figure, 'Style', 'Text', 'String', 'Height (m) =', 'Position', [70, 30, 80, 30])
%--------------------------------------------------------------------------
%Push button for execution of Task3 (opitimiser script)
uicontrol('Parent', my_figure, 'tag', 'run', 'Style', 'pushbutton', 'Position', [160,10,100,20], 'string', 'RUN', 'callback', 'Opti_final')
When i try to run the GUI and put in numbers is comes back with the error messages 'For colon operator with char operands, first and last operands must be char Error in Opti_final (line 41) for ri=ris:ristep:rie;' and 'Error while evaluating UIControl Callback.'. I have tried changing the inputs and changing the tags but i can't seem to get it to work.

采纳的回答

Guillaume
Guillaume 2020-3-1
From your code:
ris=(get(findobj('tag','ris'),'string'));
ristep=(get(findobj('tag','ristep'),'string'));
We've got unnecessary () that in my opinion don't help with readability but clearly, both ris and ristep are char vectors since you don't convert them to numbers with ideally str2double instead of str2num.
So, yes the
for ri=ris:ristep:rie;
is going to complain about char operands. Most likely, you've forgotten the conversion to number in the above lines.
As Ajay said, the ; at the end of the for line is completely unnecessary, it's just clutter again. Matlab's editor normally warns you of these things by putting a squigly line underneath and putting an orange line in the right margin. You should pay heed to these warnings.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by