fprintf problems with new lines

7 views (last 30 days)
Chett Manly
Chett Manly on 24 Oct 2021
Commented: dpb on 25 Oct 2021
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
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;
%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;
ng = n-1;
tg = (n-1)*T;
vneg = -v;
aneg = -a;
[Vmax,Tmax] = max(vneg);
VT180 = vneg(18000);
H180 = H(18000);
VT260 = vneg(26000);
H260 = H(26000);
Tfinal = t(end);
Tmax2 = Tmax/100
%% plot jibberish
ylabel('Height Kms')
yyaxis right
hold on
title('Felix Skydive')
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)
Image Analyst
Image Analyst on 24 Oct 2021
@Chett Manly, see my answer below. You need the \n inside one string, not two strings and brackets.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 24 Oct 2021
Instead of brackets and using two separate strings:
fprintf(['--------------- ---------------------- ------------------------------------','\n'])
have no brackets and put the \n inside only one format specifier string:
fprintf('--------------- ---------------------- ------------------------------------\n')
dpb on 25 Oct 2021
fprintf(['--------- -------------- --------------------------','\n'])
Sytlistic Comments:
  1. MATLAB includes (as of R2016b) the "syntactic sugar" newline function.
  2. Things like the above can be written somewhat more easily than by counting out and explicitly writing all the individual '-' characters as
fprintf([repelem('-',15) blanks(3) repelem('-',23) blanks(3) repelem('-',36) newline])
The latter form is especially useful in cases of repeated formatting fields of long records, for example a csv file of N variables might look something like
fmt=[repmat('%6.1f,',1,size(X,2)-1,1) '%6.1f' newline];
Sample usage might be--
>> X=randi(20,3)/10
X =
1.10 1.20 1.50
1.50 1.50 1.50
1.50 1.90 0.70
>> fmt=[repmat('%6.1f,',1,size(X,2)-1,1) '%6.1f' newline];
>> fprintf(1,fmt,X.')
1.1, 1.2, 1.5
1.5, 1.5, 1.5
1.5, 1.9, 0.7
This is still kludgy but is best one can to with the ill-conceived sequencing of the C format string that by putting the variable type string first (instead of following the well-established lead of FORTRAN) doesn't allow for a repeat field (FORMAT would allow one to write 2F6.1 instead).
Of course, for a simple csv file, there are builtin routines that will do this for you; the point of the exercise is to illustrate the use of formatting in more general terms than blind copying of characters.
In the above case of trying to reproduce a table, the spacings could have been determined and the counts computed to include the spacing in the field widths instead of counting and incorporating explicit blanks.
You can also specificy the field width and/or precision with '*' in the format string and pass them in the argument list along with the associated variable --
3.1416 2.718282
NB: here had to add the '\n' as additional output record as otherwise inside the formatting string would apply to each element and output a column instead.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!