fprintf problems with new lines
이전 댓글 표시
I am trying to make my data outputs look like the example and am about 90% of the way there but the caviat is that I can only use up to 6 fprintf commands.

If I can start a new line and complete the title block in two lines then the data can have a line each but the \n command wasn't generating a new line. I can make it kinda work by inputting all the data into one fprintf command but then I can't have the line titles in there.
clear; close all; clc
G = 9.80665; %gravity
D = 0.1;
v0 = 0; %Start velocity
h0 = 38969; %start height
t0 = 0; %start time
T = 0.01;
A = 0.7; %area pre parachute
m = 118; %mass
%p = 1.2; %rho
C = 1; %drag
%% part a
v(1) = v0;
H(1) = h0;
t(1) = T;
n = 1;
%
% vc = sqrt(2*m*g/(p*C*A))
% tc = vc/g
% hc = vc*tc
while 1
if H(n) < 1043 %break when ground is reached
break
end
Hkm = H(end)/1000;
g = G*6356.766.^2/(6356.766+Hkm).^2;
p = 1.2241*exp(-(Hkm/11.661)-(Hkm/18.192)^2+(Hkm/29.235)^3);
vc = sqrt(2*m*g/(p*C*A));
tc = vc/g;
hc = vc*tc;
if n > 26000 %50 sec parachute Drag increase
A = 50;
C = 3.17;
end
%a(n) = -g - D*v(n)*abs(v(n));
a(n) = -g*(1-v(n)^2/((vc^2)));
%a(n) = g*(vc^2-v(n)^2)/(vc*cosh(t-t0/tc))+v0*sinh(t-t0/tc);
v(n+1) = v(n) + a(n)*T;
%v(n+1) = vc*(v0/vc)+tanh(t-t0/tc)/(1+v0/vc*tanh(t-t0/tc));
H(n+1) = H(n) + v(n)*T;
n = n+1;
t(n) = n/100;
end
ng = n-1;
tg = (n-1)*T;
a(end+1)=0;
vneg = -v;
aneg = -a;
[Vmax,Tmax] = max(vneg);
Hmax=H(Tmax);
VT180 = vneg(18000);
H180 = H(18000);
VT260 = vneg(26000);
H260 = H(26000);
Tfinal = t(end);
Tmax2 = Tmax/100
%% plot jibberish
plot(t,H,'r-',Tmax2,Hmax,'bo');
xlabel('Time/sec')
ylabel('Height Kms')
yyaxis right
plot(t,vneg,'k-',180,VT180,'go',260,VT260,'mo')
hold on
xlabel('Time/sec')
ylabel('M/sec')
title('Felix Skydive')
legend('height','Vmax','Velocity','VT180','VT260')
grid on
%% print command
fprintf('notes observed calculated\n')
fprintf(' T V H T V H\n')
fprintf(['--------------- ---------------------- ------------------------------------','\n'])
fprintf(' %6.0f %6.4f %6.0f %6.0f %6.4f %6.0f \n',t0, v0,h0,t0,v0,h0,180,79.17,7619,180,VT180,H180,260,53.19,2567,260,VT260,H260,558,0.00,1043,Tfinal,0.00,1043)
댓글 수: 5
Image Analyst
2021년 10월 24일
Why do you have this weird requirement of how many fprintf() calls you can make? What's the point of that???
Chett Manly
2021년 10월 24일
dpb
2021년 10월 24일
You can write it all in one call to fprintf if you really, really want.
I don't recomend it as coding style, but it's certainly doable.
Chett Manly
2021년 10월 24일
편집: Chett Manly
2021년 10월 24일
Image Analyst
2021년 10월 24일
@Chett Manly, see my answer below. You need the \n inside one string, not two strings and brackets.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!