errors in sortrow in matlabr2020

15 views (last 30 days)
Sura Hatem Basir Al-Ani
Sura Hatem Basir Al-Ani on 5 Mar 2021
Edited: Cris LaPierre on 5 Mar 2021
%Initialisation---------------------------------------
clc;clear;format short g;format compact;
%-------------------------------------------------
%file name: Mat_Data
%thickness and cost factor
%INPUTS-1: 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];
%INPUTS-2: Physical inputs
P_bar=str2double(get(findobj('tag','Pressure'),'string')); %Internal Pressure of pressure vessel (Bar)
C=str2double(get(findobj('tag','Capacity'),'string')); %Capacity (Litres)
n_d=str2double(get(findobj('tag','Safety Factor'),'string')); %safety factor
L_t=str2double(get(findobj('tag','Length Constraint'),'string')); %length of pressure vessel (m)
H=str2double(get(findobj('tag','Height Constraint'),'string')); %Height of pressure vessel (m)
step=str2double(get(findobj('tag','step'),'string')); %step for loop
%conversion
P=P_bar*10^5; %conversion of Mpa to Pa
Vi=C*10^-3; %conversion of litres to m^3
%------------------------------------------------------------------------------------
%Errors
sol=[];
if P_bar <= 0
set(findobj('tag','errors'),'string','Un valid value of internal pressure')
clc
return
elseif isempty (P_bar) == true
set(findobj('tag','errors'),'string','Un valid value of internal pressure')
clc
return
end
if L_t <= 0
set(findobj('tag','errors'),'string','Un valid value of the lenght')
elseif isempty (L_t) == true
set(findobj('tag','errors'),'string','Un valid value of the lenght')
clc
return
end
if n_d <= 0
set(findobj('tag','errors'),'string','Un valid value of safety factor')
elseif isempty (n_d) == true
set(findobj('tag','errors'),'string','Un valid value of safety factor')
clc
return
end
if C <= 0
set(findobj('tag','errors'),'string','Un valid value of the capacity')
elseif isempty (C) == true
set(findobj('tag','errors'),'string','Un valid value of the capacity')
clc
return
end
if H <= 0
set(findobj('tag','errors'),'string','Un valid value of the height')
elseif isempty (H) == true
set(findobj('tag','errors'),'string','Un valid value of the height')
clc
return
end
if step <= 0
set(findobj('tag','errors'),'string','Un valid value of the step radius')
elseif isempty (step) == true
set(findobj('tag','errors'),'string','Un valid value of the step radius')
clc
return
elseif isempty (step) > 1
set(findobj('tag','errors'),'string','Un valid value of the step radius')
clc
return
end
%Calculations
for k=1:length(material)
CF=material(k).CF;
s_y=material(k).Sy*10e6;
allow_stress=s_y/n_d;
end
for x=1:3:1,length(material(k).t)
t=material(k).t(x)/1000;
z=H/2-t;
end
for r_i=0:step:z
r_o=r_i+t;
L=(Vi/(pi*r_i^2))-(4*r_i/3);
L_t=L+2*r_o;
end
if t/r_i >= 0.05
stress_t=((r_i^2*P)/(r_o^2-r_i^2))*(1+(r_o^2/r_i^2));
stress_r=((r_i^2*P)/(r_o^2-r_i^2))*(1-(r_o^2/r_i^2));
stress_l=((r_i^2*P)/(r_o^2-r_i^2));
type=1;
else
stress_t=P*r_i/t;
stress_r=0;
stress_l=P*r_i/(2*t);
type=2;
end
for stress_VM= sqrt(((stress_t-stress_r)^2+(stress_t-stress_l)^2+(stress_l-stress_r)^2)/2)
end
if stress_VM <= allow_stress && L>0
V_mat= 2*pi*r_i*t*L+4*pi*r_i^2*t;
CI=V_mat*CF;
stress_alldisp=stress_all/10e6;
stress_VMdisp=stress_VM/10e6;
L_tDisp=L+2*r_o;
Hdisp=2*r_o;
sol=[sol;r_i,r_o,t,stress_alldisp,stress_VMdisp,V_mat,CI,L_tDisp,Hdisp,type];
end
sol;
sol_sorted=sortrows(sol,8);
opt_sol=sol_sorted(1,:);
%outputs
set(findobj('tag','Inner radius'),'string', (sol_sorted(1,1)))
set(findobj('tag','Material number'),'string', (sol_sorted(1,2)))
set(findobj('tag','Thickness'),'string', (sol_sorted(1,3)))
set(findobj('tag','Total lenght'),'string', (sol_sorted(1,4)))
set(findobj('tag','Allowable Stress'),'string', (sol_sorted(1,5)))
set(findobj('tag','Von Mises Stress'),'string', (sol_sorted(1,6)))
set(findobj('tag','Vessel material volume'),'string', (sol_sorted(1,7)))
set(findobj('tag','Vessel Cost Index'),'string', (sol_sorted(1,8)))
set(findobj('tag','Height'),'string', (sol_sorted(1,9)))
set(findobj('tag','Outer radius'),'string', (sol_sorted(1,10)))
set(findobj('tag','Thickness type'),'string', (sol_sorted(1,11)))
%GUI Group project
%Main figure containing the uicontrols
My_figure = figure('Position', [100, 100, 100, 780]);
%--------------------------------------------------------------------------
%EDIT BOX FOR DESIGN PARAMETER INPUTS
uicontrol('Parent', My_figure, 'tag', 'internalPressure', 'Style', 'edit', 'Position', [200, 680,100,20])
uicontrol('Parent', My_figure, 'tag', 'Capacity', 'Style', 'edit', 'Position', [200, 620,100,20])
uicontrol('Parent', My_figure, 'tag', 'Safety Factor', 'Style', 'edit', 'Position', [200, 440,100,20])
uicontrol('Parent', My_figure, 'tag', 'Height', 'Style', 'edit', 'Position', [200, 500,100,20])
uicontrol('Parent', My_figure, 'tag', 'Length', 'Style', 'edit', 'Position', [200, 560,100,20])
uicontrol('Parent', My_figure, 'tag', 'step', 'Style', 'edit', 'Position', [200, 380,100,20])
%----------------------------------------------------------------------------------------------------------
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Capacity (litres)=', 'Position', [80, 680,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Safety Factor =', 'Position', [80, 440,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Height(m) =', 'Position', [80,500,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Lenght(m) =', 'Position', [80, 560,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'step', 'Position', [80,380, 100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'internalPressure (bar) =', 'Position', [80, 680,100,20])
%---------------------------------------------------------------------------------------------------------------
%Edit boxes for Display the results
uicontrol('Parent', My_figure,'tag','Inner radius','Style','edit','Position',[800,620,100,20])
uicontrol('Parent', My_figure, 'tag','Material number', 'Style', 'edit', 'Position', [800,200,100,20])
uicontrol('Parent', My_figure, 'tag', 'Thickness', 'Style', 'edit', 'Position', [800,440,100,20])
uicontrol('Parent', My_figure, 'tag', 'Total lenght', 'Style', 'edit', 'Position', [800, 140,100,20])
uicontrol('Parent', My_figure, 'tag', 'Allowable stress', 'Style', 'edit', 'Position', [800,320,100,20])
uicontrol('Parent', My_figure, 'tag', 'Von mises stress', 'Style', 'edit', 'Position', [800, 260,100,20])
uicontrol('Parent', My_figure, 'tag', 'Vessel material volume', 'Style', 'edit', 'Position', [800, 560,100,20])
uicontrol('Parent', My_figure, 'tag', 'Vessel cost index', 'Style', 'edit', 'Position', [800,500,100,20])
uicontrol('Parent', My_figure, 'tag', 'Height', 'Style', 'edit', 'Position', [800,80,100,20])
uicontrol('Parent', My_figure,'tag','Outer radius','Style','edit','Position',[800,680,100,20])
uicontrol('Parent', My_figure, 'tag', 'Thickness type', 'Style', 'edit', 'Position', [800,380,100,20])
%--------------------------------------------------------------------------
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Inner radius(m)', 'Position', [680, 620,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Material number', 'Position', [680,200,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Thickness (m)', 'Position', [680,440,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Total lenght (m)', 'Position', [680, 140, 100, 20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Allowable Stress (Pa)', 'Position', [680,320,100, 20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Von Mises Stress (Pa)', 'Position', [680,260,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', ' Vessel material volume(Litres)', 'Position', [650,560,150,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', ' Vessel Cost Index', 'Position', [680,500,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Height (m)', 'Position', [680,80, 100, 20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Outer radius(m)', 'Position', [680, 680,100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Thickness type', 'Position', [680, 380,100, 20])
%--------------------------------------------------------------------------------------
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Inputs', 'Position', [200,740, 100,20])
uicontrol('Parent', My_figure, 'Style', 'Text', 'String', 'Outputs', 'Position', [800,740, 100,20])
%Push button for execution of Task3 (opitimiser script)
uicontrol('Parent', My_figure, 'tag', 'run', 'Style', 'pushbutton', 'Position', [450,340,100,20], 'string', 'RUN', 'callback', 'mat_data')
this is my code , i am trying to run it , but it shows me a read messege below, how can i deal with it?
'Error using matlab.internal.math.sortrowsParseInputs
Column sorting vector must contain integers with absolute value between 1 and the number of columns in the first argument.
Error in sortrows (line 64)
[col, nanflag, compareflag] = matlab.internal.math.sortrowsParseInputs(ismatrix(A),size(A,2),A,varargin{:});

Answers (2)

Steven Lord
Steven Lord on 5 Mar 2021
"Column sorting vector must contain integers with absolute value between 1 and the number of columns in the first argument."
How many columns does the array you're passing into sortrows as the first argument have? Since I only see one sortrows call in your code:
sol_sorted=sortrows(sol,8);
what is the size of sol? Based on the error it has seven or fewer columns.
  3 Comments
Steven Lord
Steven Lord on 5 Mar 2021
What is the seventh element of the following vector?
x = [1 2 3 4 5 6]
The question doesn't make sense.
You cannot sort an array by its eighth column if the array doesn't have an eighth column. Just before that sortrows call add this line of code:
fprintf("The size of sol is %s.\n", mat2str(size(sol)))
When you run your code and get the error, show us what gets displayed for the size of sol immediately before the error occurs. It will include two or more numbers in square brackets, and the second will be less than 8. That's the number of columns in sol.
Sura Hatem Basir Al-Ani
Sura Hatem Basir Al-Ani on 5 Mar 2021
it shows me this :
The size of sol is [0 0].
so why is that?

Sign in to comment.


Cris LaPierre
Cris LaPierre on 5 Mar 2021
When I run your code, your variable sol is empty. This is why it is throwing an error. You are asking it to sort by column 8, but there are no columns at all.
Check the conditional statements in your if statements, as it appears they are never true, so no values get added to sol. Part of the problem with that is this line
for stress_VM= sqrt(((stress_t-stress_r)^2+(stress_t-stress_l)^2+(stress_l-stress_r)^2)/2)
end
Besides not being proper use of a for loop, the resulting value of stress_VM is NaN. This, in turn, is due to stress_t and stress_l also being NaN.
Not sure how you are running this, but I obviously don't have access to any of the objects your code is looking for, so it might just be due to that I am getting so many variables with NaN values.
  5 Comments
Cris LaPierre
Cris LaPierre on 5 Mar 2021
Edited: Cris LaPierre on 5 Mar 2021
Sorry, that didn't help. I get a completely different error.
This appears to be part of a gui, but without the figure file, almost all the variables have a value of NaN. For example, all of these inputs are NaN since the objects don't exist.
%INPUTS-2: Physical inputs
P_bar=str2double(get(findobj('tag','Pressure'),'string')); %Internal Pressure of pressure vessel (Bar)
C=str2double(get(findobj('tag','Capacity'),'string')); %Capacity (Litres)
n_d=str2double(get(findobj('tag','Safety Factor'),'string')); %safety factor
L_t=str2double(get(findobj('tag','Length Constraint'),'string')); %length of pressure vessel (m)
H=str2double(get(findobj('tag','Height Constraint'),'string')); %Height of pressure vessel (m)
step=str2double(get(findobj('tag','step'),'string')); %step for loop
Cris LaPierre
Cris LaPierre on 5 Mar 2021
Edited: Cris LaPierre on 5 Mar 2021
I see all your figure creation is at the bottom of your code. Since that runs after the code at the top, those objects don't get created until after. Even when they do get created, they have no default values. Since you have no callbacks that I can see, all the code runs at once, so the variables are still assigned NaN.
It looks like you are trying to build an app programmatically. Have you tried using app designer? You many find this an easier way to complete this project. There is a getting started tutorial that comes with it, as well as examples to look at.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!