how to use sprintf to an array ?

49 views (last 30 days)
Arif
Arif on 14 Apr 2024 at 15:40
Commented: Voss on 19 Apr 2024 at 15:16
Hi guys, im trying to make some text using sprintf, here i give example below:
Basically I want all datas from variable ID,X,Z in cell array comes out one bye one to the txtline(ij). Is it possible?
Here i give the code :
clear;
clc;
H = 5;
L = 15;
Ns = 2;
Nb = 1;
PortalHeight = H;
Bayspan = L;
NumberStory = Ns;
NumberBay = Nb;
% Variable1
h = [0.8,1.00,1.20,1.40,1.6,1.80];
% Variable2
Lss = [2.00,2.40,2.80,3.20,3.60,4.00];
% Variable3
Nss = [2,3,4];
% Definition
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
% % Xbase = zeros(1,length(Totalvariant));
% % Zbase = zeros(1,length(Totalvariant));
% % Xcol = zeros(1,length(Totalvariant));
% % Zcol = zeros(1,length(Totalvariant));
% % Xos = zeros(1,length(Totalvariant));
% % Zos = zeros(1,length(Totalvariant));
% % Xss = zeros(1,length(Totalvariant));
% % Zss = zeros(1,length(Totalvariant));
% % X = zeros(1,length(Totalvariant));
% % Z = zeros(1,length(Totalvariant));
% % jointbase = zeros(1,length(Totalvariant));
% % jointcolumn = zeros(1,length(Totalvariant));
% % jointordinarysegment = zeros(1,length(Totalvariant));
% % jointtopordinarysegment = zeros(1,length(Totalvariant));
% % jointbottomordinarysegment = zeros(1,length(Totalvariant));
% % jointspecialsegment = zeros(1,length(Totalvariant));
% % jointtopspecialsegment = zeros(1,length(Totalvariant));
% % jointbottomspecialsegment = zeros(1,length(Totalvariant));
ijk = 0;
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1;
Variantmodel{ijk,:} = table(ijk,h(i),Lss(j),Nss(k),'VariableNames',{'VariantID','Height','Length','Number'});
%% Base(base)
Xbase{ijk,:} = L*(0:NumberBay);
Zbase{ijk,:} = repelem(0,1,length(Xbase{ijk,:}));
%% Column(col)
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(i)),1,length(Xbotbeam));
% replicate [X] to be the same size as [Z] for plotting:
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
los = Los/Nos;
totalspan{ijk,:} = los*Nos*2+Lss(j);
if totalspan{ijk,:} == 15.000
checkspan{ijk,:} = "correct span";
else
checkspan{ijk,:} = "span must be 15m";
end
% top and bottom joint of ordinary segment(os)
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(j)/2)+los*(0:Nos-1)].'+L*(0:(NumberBay-1)),1,[]);
XBottomOrdinarySegment = XTopOrdinarySegment;
ZTopOrdinarySegment = repelem(H*(1:NumberStory),1,2*Nos*NumberBay);
ZBottomOrdinarySegment = repelem(H*(1:NumberStory)-h(i),1,2*Nos*NumberBay);
% replicate [X] to be the same size as [Z] for plotting:
Xos{ijk,:} = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos{ijk,:} = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
%% Special Segment
% calculate span length of special segment
lss = Lss(j)/Nss(k);
% top and bottom joint of special segment(ss)
XTopSpecialSegment = reshape((lss*(0:Nss(k))+L/2-Lss(j)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(i),1,length(XBottomSpecialSegment));
% replicate [X] to be the same size as [Z] for plotting:
Xss{ijk,:} = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss{ijk,:} = [ZTopSpecialSegment,ZBottomSpecialSegment];
%% Plot Joint
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
%check angle
tangent = (Lss(j)/Nss(k))/h(i);
anglespecialsegment(ijk) = atand(tangent);
%% Name Joint
jointbase{ijk} = table((1:numel(Xbase{ijk,:}))',Xbase{ijk,:}',Zbase{ijk,:}','VariableNames',{'ID','X','Z'});
jointcolumn{ijk} = table((max(jointbase{ijk}.ID)+1:numel(Xcol{ijk,:})+max(jointbase{ijk}.ID))',Xcol{ijk,:}',Zcol{ijk,:}','VariableNames',{'ID','X','Z'});
jointordinarysegment{ijk} = table((max(jointcolumn{ijk}.ID)+1:numel(Xos{ijk,:})+max(jointcolumn{ijk}.ID))',Xos{ijk,:}',Zos{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
jointbottomordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zbotbeam),:);
jointspecialsegment{ijk} = table((max(jointbottomordinarysegment{ijk}.ID)+1:numel(Xss{ijk,:})+max(jointbottomordinarysegment{ijk}.ID))',Xss{ijk,:}',Zss{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zcolumn),:);
jointbottomspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zbotbeam),:);
end
end
end
%% Verify Variants Which Passed the Angle Requirement
tablecheck = table((1:numel(anglespecialsegment))',totalspan,anglespecialsegment','VariableNames',{'VariantID','Total Span','Angle'});
VariantIDPassed = tablecheck.VariantID(tablecheck.Angle>=30 & tablecheck.Angle<=60);
jointbase = table((1:numel(jointbase))',jointbase','VariableNames',{'VariantID','Joint Base'});
jointbase = jointbase(ismember(jointbase.VariantID,VariantIDPassed),:);
jointcolumn = table((1:numel(jointcolumn))',jointcolumn','VariableNames',{'VariantID','Joint Column'});
jointcolumn = jointcolumn(ismember(jointcolumn.VariantID,VariantIDPassed),:);
jointtopordinarysegment = table((1:numel(jointtopordinarysegment))',jointtopordinarysegment','VariableNames',{'VariantID','Joint Top Ordinary Segment'});
jointtopordinarysegment = jointtopordinarysegment(ismember(jointtopordinarysegment.VariantID,VariantIDPassed),:);
jointbottomordinarysegment = table((1:numel(jointbottomordinarysegment))',jointbottomordinarysegment','VariableNames',{'VariantID','Joint Bottom Ordinary Segment'});
jointbottomordinarysegment = jointbottomordinarysegment(ismember(jointbottomordinarysegment.VariantID,VariantIDPassed),:);
jointtopspecialsegment = table((1:numel(jointtopspecialsegment))',jointtopspecialsegment','VariableNames',{'VariantID','Joint Top Special Segment'});
jointtopspecialsegment = jointtopspecialsegment(ismember(jointtopspecialsegment.VariantID,VariantIDPassed),:);
jointbottomspecialsegment = table((1:numel(jointbottomspecialsegment))',jointbottomspecialsegment','VariableNames',{'VariantID','Joint Bottom Special Segment'});
jointbottomspecialsegment = jointbottomspecialsegment(ismember(jointbottomspecialsegment.VariantID,VariantIDPassed),:);
%% Print data in cell array one-by-one
ij = 0
for i = 1:height(jointbottomspecialsegment)
BottomSS = jointbottomspecialsegment.("Joint Bottom Special Segment")(i)
for j = 1:height(BottomSS{1,1})
ij = ij+1
Joint = BottomSS{1,1}.ID(j)
X = BottomSS{1,1}.X(j)
Z =BottomSS{1,1}.Z(j)
txtline(ij) = sprintf(' Joint=%d CoordSys=GLOBAL CoordType=Cartesian XorR=%d Y=0 Z=%d SpecialJt=No GlobalX=%d GlobalY=0 GlobalZ=%d');
end
end

Accepted Answer

Voss
Voss on 14 Apr 2024 at 16:06
Yes, it is possible.
However, your sprintf format specification has 5 formatting operators (all %d), but you only have 3 numbers (ID,X,Z) to go into it. I'm not sure what the other two numbers are supposed to be or whether the format specification is supposed to have only 3 formatting operators. Here I'll use X and Z two times each to fill it in, and you can adjust it as needed:
clear;
clc;
H = 5;
L = 15;
Ns = 2;
Nb = 1;
PortalHeight = H;
Bayspan = L;
NumberStory = Ns;
NumberBay = Nb;
% Variable1
h = [0.8,1.00,1.20,1.40,1.6,1.80];
% Variable2
Lss = [2.00,2.40,2.80,3.20,3.60,4.00];
% Variable3
Nss = [2,3,4];
% Definition
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
% % Xbase = zeros(1,length(Totalvariant));
% % Zbase = zeros(1,length(Totalvariant));
% % Xcol = zeros(1,length(Totalvariant));
% % Zcol = zeros(1,length(Totalvariant));
% % Xos = zeros(1,length(Totalvariant));
% % Zos = zeros(1,length(Totalvariant));
% % Xss = zeros(1,length(Totalvariant));
% % Zss = zeros(1,length(Totalvariant));
% % X = zeros(1,length(Totalvariant));
% % Z = zeros(1,length(Totalvariant));
% % jointbase = zeros(1,length(Totalvariant));
% % jointcolumn = zeros(1,length(Totalvariant));
% % jointordinarysegment = zeros(1,length(Totalvariant));
% % jointtopordinarysegment = zeros(1,length(Totalvariant));
% % jointbottomordinarysegment = zeros(1,length(Totalvariant));
% % jointspecialsegment = zeros(1,length(Totalvariant));
% % jointtopspecialsegment = zeros(1,length(Totalvariant));
% % jointbottomspecialsegment = zeros(1,length(Totalvariant));
ijk = 0;
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1;
Variantmodel{ijk,:} = table(ijk,h(i),Lss(j),Nss(k),'VariableNames',{'VariantID','Height','Length','Number'});
%% Base(base)
Xbase{ijk,:} = L*(0:NumberBay);
Zbase{ijk,:} = repelem(0,1,length(Xbase{ijk,:}));
%% Column(col)
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(i)),1,length(Xbotbeam));
% replicate [X] to be the same size as [Z] for plotting:
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
los = Los/Nos;
totalspan{ijk,:} = los*Nos*2+Lss(j);
if totalspan{ijk,:} == 15.000
checkspan{ijk,:} = "correct span";
else
checkspan{ijk,:} = "span must be 15m";
end
% top and bottom joint of ordinary segment(os)
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(j)/2)+los*(0:Nos-1)].'+L*(0:(NumberBay-1)),1,[]);
XBottomOrdinarySegment = XTopOrdinarySegment;
ZTopOrdinarySegment = repelem(H*(1:NumberStory),1,2*Nos*NumberBay);
ZBottomOrdinarySegment = repelem(H*(1:NumberStory)-h(i),1,2*Nos*NumberBay);
% replicate [X] to be the same size as [Z] for plotting:
Xos{ijk,:} = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos{ijk,:} = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
%% Special Segment
% calculate span length of special segment
lss = Lss(j)/Nss(k);
% top and bottom joint of special segment(ss)
XTopSpecialSegment = reshape((lss*(0:Nss(k))+L/2-Lss(j)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(i),1,length(XBottomSpecialSegment));
% replicate [X] to be the same size as [Z] for plotting:
Xss{ijk,:} = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss{ijk,:} = [ZTopSpecialSegment,ZBottomSpecialSegment];
%% Plot Joint
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
%check angle
tangent = (Lss(j)/Nss(k))/h(i);
anglespecialsegment(ijk) = atand(tangent);
%% Name Joint
jointbase{ijk} = table((1:numel(Xbase{ijk,:}))',Xbase{ijk,:}',Zbase{ijk,:}','VariableNames',{'ID','X','Z'});
jointcolumn{ijk} = table((max(jointbase{ijk}.ID)+1:numel(Xcol{ijk,:})+max(jointbase{ijk}.ID))',Xcol{ijk,:}',Zcol{ijk,:}','VariableNames',{'ID','X','Z'});
jointordinarysegment{ijk} = table((max(jointcolumn{ijk}.ID)+1:numel(Xos{ijk,:})+max(jointcolumn{ijk}.ID))',Xos{ijk,:}',Zos{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
jointbottomordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zbotbeam),:);
jointspecialsegment{ijk} = table((max(jointbottomordinarysegment{ijk}.ID)+1:numel(Xss{ijk,:})+max(jointbottomordinarysegment{ijk}.ID))',Xss{ijk,:}',Zss{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zcolumn),:);
jointbottomspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zbotbeam),:);
end
end
end
%% Verify Variants Which Passed the Angle Requirement
tablecheck = table((1:numel(anglespecialsegment))',totalspan,anglespecialsegment','VariableNames',{'VariantID','Total Span','Angle'});
VariantIDPassed = tablecheck.VariantID(tablecheck.Angle>=30 & tablecheck.Angle<=60);
jointbase = table((1:numel(jointbase))',jointbase','VariableNames',{'VariantID','Joint Base'});
jointbase = jointbase(ismember(jointbase.VariantID,VariantIDPassed),:);
jointcolumn = table((1:numel(jointcolumn))',jointcolumn','VariableNames',{'VariantID','Joint Column'});
jointcolumn = jointcolumn(ismember(jointcolumn.VariantID,VariantIDPassed),:);
jointtopordinarysegment = table((1:numel(jointtopordinarysegment))',jointtopordinarysegment','VariableNames',{'VariantID','Joint Top Ordinary Segment'});
jointtopordinarysegment = jointtopordinarysegment(ismember(jointtopordinarysegment.VariantID,VariantIDPassed),:);
jointbottomordinarysegment = table((1:numel(jointbottomordinarysegment))',jointbottomordinarysegment','VariableNames',{'VariantID','Joint Bottom Ordinary Segment'});
jointbottomordinarysegment = jointbottomordinarysegment(ismember(jointbottomordinarysegment.VariantID,VariantIDPassed),:);
jointtopspecialsegment = table((1:numel(jointtopspecialsegment))',jointtopspecialsegment','VariableNames',{'VariantID','Joint Top Special Segment'});
jointtopspecialsegment = jointtopspecialsegment(ismember(jointtopspecialsegment.VariantID,VariantIDPassed),:);
jointbottomspecialsegment = table((1:numel(jointbottomspecialsegment))',jointbottomspecialsegment','VariableNames',{'VariantID','Joint Bottom Special Segment'});
jointbottomspecialsegment = jointbottomspecialsegment(ismember(jointbottomspecialsegment.VariantID,VariantIDPassed),:);
%% Print data in cell array one-by-one
ij = 0;
txtline = strings(0,1); % make txtline a string array
for i = 1:height(jointbottomspecialsegment)
BottomSS = jointbottomspecialsegment.("Joint Bottom Special Segment")(i);
for j = 1:height(BottomSS{1,1})
ij = ij+1;
Joint = BottomSS{1,1}.ID(j);
X = BottomSS{1,1}.X(j);
Z = BottomSS{1,1}.Z(j);
txtline(ij,1) = sprintf(' Joint=%d CoordSys=GLOBAL CoordType=Cartesian XorR=%d Y=0 Z=%d SpecialJt=No GlobalX=%d GlobalY=0 GlobalZ=%d',Joint,X,Z,X,Z);
end
end
txtline
txtline = 578x1 string array
" Joint=81 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=82 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=83 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=84 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=83 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=84 CoordSys=GLOBAL CoordType=Cartesian XorR=7.166667e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.166667e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=7.833333e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.833333e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=87 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=88 CoordSys=GLOBAL CoordType=Cartesian XorR=7.166667e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.166667e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=89 CoordSys=GLOBAL CoordType=Cartesian XorR=7.833333e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.833333e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=90 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=7 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7 GlobalY=0 GlobalZ=4.200000e+00" " Joint=87 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=88 CoordSys=GLOBAL CoordType=Cartesian XorR=8 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8 GlobalY=0 GlobalZ=4.200000e+00" " Joint=89 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=90 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=91 CoordSys=GLOBAL CoordType=Cartesian XorR=7 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7 GlobalY=0 GlobalZ=9.200000e+00" " Joint=92 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=93 CoordSys=GLOBAL CoordType=Cartesian XorR=8 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8 GlobalY=0 GlobalZ=9.200000e+00" " Joint=94 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=81 CoordSys=GLOBAL CoordType=Cartesian XorR=6.300000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.300000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=82 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=83 CoordSys=GLOBAL CoordType=Cartesian XorR=8.700000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.700000e+00 GlobalY=0 GlobalZ=4.200000e+00" " Joint=84 CoordSys=GLOBAL CoordType=Cartesian XorR=6.300000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.300000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=9.200000e+00" " Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=8.700000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.700000e+00 GlobalY=0 GlobalZ=9.200000e+00"
You may want to use %g or %.1f or similar instead of %d for the X and Z values.
  7 Comments
Arif
Arif on 19 Apr 2024 at 8:26
''So txtline should be an array (cell array or string array) with as many elements as jointbottomspecialsegment has rows, right?'' Yes
"I think I understand that much, but why should each element of txtline contain multiple lines of text as in the example txtline(1) and txtline(2) you show?" Because i want to create many files (number of file is same with jointbottomspecialsegment's height)) with .txt extension and each file contains like this :
I already know how to export the string to be a text.file and now im trying to make variable in-array and contain string like txtline(1) and txtline(2) that i showed above.
Voss
Voss on 19 Apr 2024 at 15:16
OK. I understand now. Thank you for the explanation.
See below for one way to generate txtline like you want and write each element to a file.
clear;
clc;
H = 5;
L = 15;
Ns = 2;
Nb = 1;
PortalHeight = H;
Bayspan = L;
NumberStory = Ns;
NumberBay = Nb;
% Variable1
h = [0.8,1.00,1.20,1.40,1.6,1.80];
% Variable2
Lss = [2.00,2.40,2.80,3.20,3.60,4.00];
% Variable3
Nss = [2,3,4];
% Definition
Var1HeightofTrussBeam = h;
Var2LengthSpecialSegment = Lss;
Var3NumberSpecialSegment = Nss;
% % Xbase = zeros(1,length(Totalvariant));
% % Zbase = zeros(1,length(Totalvariant));
% % Xcol = zeros(1,length(Totalvariant));
% % Zcol = zeros(1,length(Totalvariant));
% % Xos = zeros(1,length(Totalvariant));
% % Zos = zeros(1,length(Totalvariant));
% % Xss = zeros(1,length(Totalvariant));
% % Zss = zeros(1,length(Totalvariant));
% % X = zeros(1,length(Totalvariant));
% % Z = zeros(1,length(Totalvariant));
% % jointbase = zeros(1,length(Totalvariant));
% % jointcolumn = zeros(1,length(Totalvariant));
% % jointordinarysegment = zeros(1,length(Totalvariant));
% % jointtopordinarysegment = zeros(1,length(Totalvariant));
% % jointbottomordinarysegment = zeros(1,length(Totalvariant));
% % jointspecialsegment = zeros(1,length(Totalvariant));
% % jointtopspecialsegment = zeros(1,length(Totalvariant));
% % jointbottomspecialsegment = zeros(1,length(Totalvariant));
ijk = 0;
for i = 1:numel(h)
for j = 1:numel(Lss)
for k = 1:numel(Nss)
ijk = ijk+1;
Variantmodel{ijk,:} = table(ijk,h(i),Lss(j),Nss(k),'VariableNames',{'VariantID','Height','Length','Number'});
%% Base(base)
Xbase{ijk,:} = L*(0:NumberBay);
Zbase{ijk,:} = repelem(0,1,length(Xbase{ijk,:}));
%% Column(col)
Xcolumn = L*(0:NumberBay);
Zcolumn = repelem(H*(1:NumberStory),1,length(Xcolumn));
Xbotbeam = L*(0:NumberBay);
Zbotbeam = repelem((H*(1:NumberStory)-h(i)),1,length(Xbotbeam));
% replicate [X] to be the same size as [Z] for plotting:
Xcol{ijk,:} = repmat([Xcolumn,Xbotbeam],1,NumberStory);
Zcol{ijk,:} = [Zcolumn,Zbotbeam];
%% Ordinary Segment
% calculate span length of ordinary segment
Los = (L-Lss(j))/2;
Nos = Los/h(i);
Nos = round(Nos);
los = Los/Nos;
totalspan{ijk,:} = los*Nos*2+Lss(j);
if totalspan{ijk,:} == 15.000
checkspan{ijk,:} = "correct span";
else
checkspan{ijk,:} = "span must be 15m";
end
% top and bottom joint of ordinary segment(os)
XTopOrdinarySegment = reshape([los*(1:Nos),(L/2+Lss(j)/2)+los*(0:Nos-1)].'+L*(0:(NumberBay-1)),1,[]);
XBottomOrdinarySegment = XTopOrdinarySegment;
ZTopOrdinarySegment = repelem(H*(1:NumberStory),1,2*Nos*NumberBay);
ZBottomOrdinarySegment = repelem(H*(1:NumberStory)-h(i),1,2*Nos*NumberBay);
% replicate [X] to be the same size as [Z] for plotting:
Xos{ijk,:} = repmat([XTopOrdinarySegment,XBottomOrdinarySegment],1,NumberStory);
Zos{ijk,:} = [ZTopOrdinarySegment,ZBottomOrdinarySegment];
%% Special Segment
% calculate span length of special segment
lss = Lss(j)/Nss(k);
% top and bottom joint of special segment(ss)
XTopSpecialSegment = reshape((lss*(0:Nss(k))+L/2-Lss(j)/2).'+L*(0:(NumberBay-1)),1,[]);
XBottomSpecialSegment = XTopSpecialSegment;
ZTopSpecialSegment = repelem(H*(1:NumberStory),1,length(XTopSpecialSegment));
ZBottomSpecialSegment = repelem(H*(1:NumberStory)-h(i),1,length(XBottomSpecialSegment));
% replicate [X] to be the same size as [Z] for plotting:
Xss{ijk,:} = repmat([XTopSpecialSegment,XBottomSpecialSegment],1,NumberStory);
Zss{ijk,:} = [ZTopSpecialSegment,ZBottomSpecialSegment];
%% Plot Joint
X{ijk,:} = [Xbase{ijk,:},Xcol{ijk,:},Xos{ijk,:},Xss{ijk,:}];
Z{ijk,:} = [Zbase{ijk,:},Zcol{ijk,:},Zos{ijk,:},Zss{ijk,:}];
scatter(X{ijk,:},Z{ijk,:},'filled'),grid on
%check angle
tangent = (Lss(j)/Nss(k))/h(i);
anglespecialsegment(ijk) = atand(tangent);
%% Name Joint
jointbase{ijk} = table((1:numel(Xbase{ijk,:}))',Xbase{ijk,:}',Zbase{ijk,:}','VariableNames',{'ID','X','Z'});
jointcolumn{ijk} = table((max(jointbase{ijk}.ID)+1:numel(Xcol{ijk,:})+max(jointbase{ijk}.ID))',Xcol{ijk,:}',Zcol{ijk,:}','VariableNames',{'ID','X','Z'});
jointordinarysegment{ijk} = table((max(jointcolumn{ijk}.ID)+1:numel(Xos{ijk,:})+max(jointcolumn{ijk}.ID))',Xos{ijk,:}',Zos{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zcolumn),:);
jointbottomordinarysegment{ijk} = jointordinarysegment{ijk}(ismember(jointordinarysegment{ijk}.Z,Zbotbeam),:);
jointspecialsegment{ijk} = table((max(jointbottomordinarysegment{ijk}.ID)+1:numel(Xss{ijk,:})+max(jointbottomordinarysegment{ijk}.ID))',Xss{ijk,:}',Zss{ijk,:}','VariableNames',{'ID','X','Z'});
jointtopspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zcolumn),:);
jointbottomspecialsegment{ijk} = jointspecialsegment{ijk}(ismember(jointspecialsegment{ijk}.Z,Zbotbeam),:);
end
end
end
%% Verify Variants Which Passed the Angle Requirement
tablecheck = table((1:numel(anglespecialsegment))',totalspan,anglespecialsegment','VariableNames',{'VariantID','Total Span','Angle'});
VariantIDPassed = tablecheck.VariantID(tablecheck.Angle>=30 & tablecheck.Angle<=60);
jointbase = table((1:numel(jointbase))',jointbase','VariableNames',{'VariantID','Joint Base'});
jointbase = jointbase(ismember(jointbase.VariantID,VariantIDPassed),:);
jointcolumn = table((1:numel(jointcolumn))',jointcolumn','VariableNames',{'VariantID','Joint Column'});
jointcolumn = jointcolumn(ismember(jointcolumn.VariantID,VariantIDPassed),:);
jointtopordinarysegment = table((1:numel(jointtopordinarysegment))',jointtopordinarysegment','VariableNames',{'VariantID','Joint Top Ordinary Segment'});
jointtopordinarysegment = jointtopordinarysegment(ismember(jointtopordinarysegment.VariantID,VariantIDPassed),:);
jointbottomordinarysegment = table((1:numel(jointbottomordinarysegment))',jointbottomordinarysegment','VariableNames',{'VariantID','Joint Bottom Ordinary Segment'});
jointbottomordinarysegment = jointbottomordinarysegment(ismember(jointbottomordinarysegment.VariantID,VariantIDPassed),:);
jointtopspecialsegment = table((1:numel(jointtopspecialsegment))',jointtopspecialsegment','VariableNames',{'VariantID','Joint Top Special Segment'});
jointtopspecialsegment = jointtopspecialsegment(ismember(jointtopspecialsegment.VariantID,VariantIDPassed),:);
jointbottomspecialsegment = table((1:numel(jointbottomspecialsegment))',jointbottomspecialsegment','VariableNames',{'VariantID','Joint Bottom Special Segment'});
jointbottomspecialsegment = jointbottomspecialsegment(ismember(jointbottomspecialsegment.VariantID,VariantIDPassed),:);
N = height(jointbottomspecialsegment);
txtline = strings(N,1);
for i = 1:N
T = jointbottomspecialsegment.("Joint Bottom Special Segment"){i};
M = T{:,:};
M(:,end+[1 2]) = M(:,[2 3]); % duplicate columns 2 and 3 into columns 4 and 5
txtline(i) = sprintf(" Joint=%d CoordSys=GLOBAL CoordType=Cartesian XorR=%d Y=0 Z=%d SpecialJt=No GlobalX=%d GlobalY=0 GlobalZ=%d\n",M.');
end
txtline(1)
ans =
" Joint=81 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=82 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=83 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=84 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=9.200000e+00 Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=7.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.500000e+00 GlobalY=0 GlobalZ=9.200000e+00 Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=9.200000e+00 "
txtline(2)
ans =
" Joint=83 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=84 CoordSys=GLOBAL CoordType=Cartesian XorR=7.166667e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.166667e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=85 CoordSys=GLOBAL CoordType=Cartesian XorR=7.833333e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=7.833333e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=86 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=4.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=4.200000e+00 Joint=87 CoordSys=GLOBAL CoordType=Cartesian XorR=6.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=6.500000e+00 GlobalY=0 GlobalZ=9.200000e+00 Joint=88 CoordSys=GLOBAL CoordType=Cartesian XorR=7.166667e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.166667e+00 GlobalY=0 GlobalZ=9.200000e+00 Joint=89 CoordSys=GLOBAL CoordType=Cartesian XorR=7.833333e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=7.833333e+00 GlobalY=0 GlobalZ=9.200000e+00 Joint=90 CoordSys=GLOBAL CoordType=Cartesian XorR=8.500000e+00 Y=0 Z=9.200000e+00 SpecialJt=No GlobalX=8.500000e+00 GlobalY=0 GlobalZ=9.200000e+00 "
Then you would write each element of txtline to a different file, e.g., by using writelines in a loop.
for i = 1:N
file_name = "file" + i + ".txt";
writelines(txtline(i),file_name);
end

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!