Matlab coding issue with "fprintf" function
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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

채택된 답변
Star Strider
2021년 2월 4일
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
Adam Scott
2021년 2월 4일
편집: Walter Roberson
2021년 2월 5일
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.')
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.')
@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).
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.
Your solution is working. The error was on my side.
Thank you for your time and support. It is really appreciated.
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
