Matlab coding issue with "fprintf" function

Hi, i am trying to print the output of my code into columns and now the results print but not in their respective columns. It prints from the first value of my first variable to its last, then the 1st value of my 2nd variable to its last, etc. I want them to print in their respective columns like the example below.
x v a
-------------
x1 v1 a1
x2 v2 a2
x3 v3 a3
but what i obtain is
x v a
-------------
x1 x2 x3
v1 v2 v3
a1 a2 a3
this is my code

 Respuesta aceptada

Star Strider
Star Strider el 4 de Feb. de 2021
My version of MATLAB will not run images of code, only actual code, and I have no desire to type all of that in.
See if:
fprintf('%-4.2f %-42.f %-4.2f %-4.2f', output.')
does what you want. (Note the transposition: (.') for complex data or (') otherwise.)

11 comentarios

Adam Scott
Adam Scott el 4 de Feb. de 2021
Editada: Walter Roberson el 5 de Feb. de 2021
Hi, i tried your solution and now my values are tabulated all in the same row.
I will leave my code here maybe you can see what i did wrong. Sorry for the picture. First post in this forum.
Thank you
clear;
clc;
%Problem 1-A
%data given
m=4;
k=2500;
c=0;
x0=0.1;
xd0=-10;
n=50;
dt=0.01;
t=[0:dt:(n*dt)];
%natural frequency
wn=sqrt(k/m);
%damping ratio
tho=c/(2*sqrt(k*m));
%Calculating displacement
if tho==0 %undamped
xt=(xd0/wn)*sin(wn*t)+x0*cos(wn*t);
xdt=xd0*cos(wn*t)-x0*wn*sin(wn*t);
xddt=-x0*wn*sin(wn*t)-x0*wn^2*cos(wn*t);
end
plot(t,xt,t,xdt,t,xddt);
legend('Displacement','velocity','acceleration')
title('System responses')
xlabel('time(s)')
ylabel('x(t), v(t),a(t)')
grid on
output = [t';xt';xdt';xddt'];
fprintf('t x v a\n')
fprintf('----------------------------------\n')
fprintf('%-4.2f %-4.2f %-4.2f %-4.2f',output.')
Star Strider
Star Strider el 4 de Feb. de 2021
My pleasure!
So, I assume my change worked and it now does what you want?
If my Answer helped you solve your problem, please Accept it!
.
fprintf('%-4.2f %-4.2f %-4.2f %-4.2f\n',output.')
Adam Scott
Adam Scott el 5 de Feb. de 2021
@Star Strider No unfortunately it still doesnt tabulate as i would like. You can run the code to see that the value of "t" (time) is not tabulated in the first column.
Fixed-width formats are not magic. They do not look at all of the data ahead of time and automatically expand the width to be used for a column according to the maximum width that any element of the column will need. If you want everything to line up, you need to use a width that accomodates the maximum range including possible negative sign.
Also you used a negative width. That tells MATLAB to use left justification with the field.
1.00_
-1.00
Whereas you probably wanted
1.00
-1.00
which you can only get with right justification or 0 fill.
%Problem 1-A
%data given
m=4;
k=2500;
c=0;
x0=0.1;
xd0=-10;
n=50;
dt=0.01;
t=[0:dt:(n*dt)];
%natural frequency
wn=sqrt(k/m);
%damping ratio
tho=c/(2*sqrt(k*m));
%Calculating displacement
if tho==0 %undamped
xt=(xd0/wn)*sin(wn*t)+x0*cos(wn*t);
xdt=xd0*cos(wn*t)-x0*wn*sin(wn*t);
xddt=-x0*wn*sin(wn*t)-x0*wn^2*cos(wn*t);
end
plot(t,xt,t,xdt,t,xddt);
legend('Displacement','velocity','acceleration')
title('System responses')
xlabel('time(s)')
ylabel('x(t), v(t),a(t)')
grid on
output = [t';xt';xdt';xddt'];
fprintf('t x v a\n')
t x v a
fprintf('----------------------------------\n')
----------------------------------
fprintf('%6.2f %6.2f %6.2f %6.2f\n',output.')
0.00 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.20 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.30 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.40 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.50 0.10 -0.00 -0.10 -0.20 -0.28 -0.35 -0.39 -0.41 -0.41 -0.37 -0.32 -0.25 -0.16 -0.06 0.05 0.15 0.24 0.31 0.37 0.40 0.41 0.39 0.35 0.29 0.21 0.11 0.01 -0.09 -0.19 -0.27 -0.34 -0.39 -0.41 -0.41 -0.38 -0.33 -0.26 -0.17 -0.07 0.03 0.13 0.23 0.30 0.36 0.40 0.41 0.40 0.36 0.30 0.22 0.13 -10.00 -10.31 -9.97 -9.02 -7.51 -5.53 -3.20 -0.68 1.89 4.34 6.52 8.29 9.55 10.21 10.24 9.63 8.43 6.70 4.55 2.12 -0.44 -2.97 -5.32 -7.34 -8.90 -9.91 -10.30 -10.06 -9.18 -7.74 -5.81 -3.52 -1.02 1.55 4.02 6.25 8.08 9.41 10.16 10.27 9.75 8.62 6.95 4.86 2.46 -0.10 -2.64 -5.03 -7.10 -8.73 -9.81 -62.50 -61.18 -56.05 -47.43 -35.87 -22.08 -6.91 8.68 23.74 37.32 48.58 56.81 61.52 62.40 59.41 52.71 42.74 30.12 15.62 0.15 -15.33 -29.86 -42.53 -52.55 -59.31 -62.38 -61.57 -56.94 -48.76 -37.55 -24.01 -8.97 6.62 21.80 35.63 47.24 55.92 61.11 62.51 60.02 53.80 44.24 31.92 17.62 2.22 -13.31 -28.02 -40.98 -51.40 -58.62 -62.20
Maybe i explained myself wrong. The desired output i want is obtained in the example below ! I just want to adapt it to mine so that i can obtain a table well organised as below.
---------------------------
%Given time span
deltaT = 0.01;
n = 50;
tspan = 0:deltaT:n*deltaT;
%Given initial conditions
y0 = [100e-3 -10e-3];
m = 4;
k = 2500;
c = 0;
%Numerical solution of differential equations
[T1,Y1] = ode45(@(t,y) coupled_diff(t,y,m,k,c),tspan,y0);
t = T1;
x = Y1(:,1);
v = Y1(:,2);
a = (-k*x - c*v)/m; plot(t,x,t,v,t,a);
output = [t';x';v';a'];
fprintf('t x v a\n')
fprintf('----------------------------------\n')
fprintf('%-4.2f %-4.2f %-4.2f %-4.2f\n',output)
%Function definition
function f = coupled_diff(t,y,m,k,c)
f = zeros(2,1);
f(1) = y(2);
f(2) = (-k*y(1)-c*y(2))/m;
end
****Here we can see that the value for time (t) starts from 0 and goes up. That is the relationship i want for my example. I want the time to start from 0 and to see the respective displacement, velocity and acceleration. ***
Walter — Thank you!
Tristan — If you create ‘output’ as:
output = [t x v a];
horizontally concatenating the column vectors, and then use:
fprintf('%-4.2f %-4.2f %-4.2f %-4.2f\n',output')
(again note the transpose) the table produces the correct result, and duplicates the original ‘output’ matrix (although the format string in the fprintf call, specifically the field width, needs to be increased to produce a readable result).
Adam Scott
Adam Scott el 5 de Feb. de 2021
So @Star Strider what you are saying is that i should keep the code as
output = [t xt xdt xddt];
fprintf('t x v a\n')
fprintf('----------------------------------\n')
fprintf('%-4.2f %-4.2f %-4.2f %-4.2f\n',output')
but that i should change the "4.2" values and this will lead to my values being tabulated in the right COLUMN and not tabulated like in a book (left to right) ?
I have been using matlab for a very short period of time and i am stil confused a lot
I still do not understand what you want, since Walter’s and my solutions produce the result you said you want, that being printing the table correctly.
The approach I used here produces the first 10 rows as:
t x v a
----------------------------------
0.00 0.10 -0.01 -62.50
0.01 0.10 -0.63 -60.50
0.02 0.09 -1.21 -54.73
0.03 0.07 -1.71 -45.56
0.04 0.05 -2.11 -33.56
0.05 0.03 -2.38 -19.47
0.06 0.01 -2.49 -4.16
0.07 -0.02 -2.46 11.40
0.08 -0.04 -2.27 26.25
0.09 -0.06 -1.94 39.46
that appear to be correct, and that match the ‘output’ matrix. (I would format it differently, however that is simply an aesthetic issue to make it eassier to read.)
That is the best I can do.
Adam Scott
Adam Scott el 5 de Feb. de 2021
Your solution is working. The error was on my side.
Thank you for your time and support. It is really appreciated.
Star Strider
Star Strider el 5 de Feb. de 2021
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Feb. de 2021

Comentada:

el 5 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by