disp gives wrong format

조회 수: 2 (최근 30일)
Kelly McGuire
Kelly McGuire 2019년 10월 17일
답변: Image Analyst 2019년 10월 17일
I was making a program to calculate the vertical displacement and then display a matrix of the time and displacement outputs. The output should be in seconds and meters, but disp() is changing the decimal place when the values are printed. Why is this happening? Here is the code:
% Program that calculates vertical motion under gravity - ignoring air
% friction
% Initialize Constants
g = 9.81; % gravity acceleration - m/s^2
u = 60; % initial velocity - m/s
t = 0 : 0.01 : 12.3; % time array from 0 to 12.3 seconds in 0.01 second intervals
s = u*t - g/2*t.^2; % vertical displacement equation - meters
disp([t',s']);
plot(t,s,'k','linewidth',3);
title('Vertical Motion Under Gravity');
xlabel('Time (seconds)');
ylabel('Vertical Displacement (meters)');
grid

답변 (2개)

Wiley Mosley
Wiley Mosley 2019년 10월 17일
I was not able to recreate the issue that you described. One thing to note is depending on your settings it could be outputing a scientific notation form with a call of 1.0e-01 at the start that may be confusing.
  댓글 수: 3
Wiley Mosley
Wiley Mosley 2019년 10월 17일
For 2019b it is under the view tab - > Number Display Format
Try - short fixed decimal or long fixed decimal to prevent the extra scientific notation.
Kelly McGuire
Kelly McGuire 2019년 10월 17일
I have the 2016b academic version. I put format short before disp(), and it fixed the format.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2019년 10월 17일
Try fprintf() instead of disp():
fprintf('%6.2f, %9.6f\n', [t; s]);
With fprintf() you can get exactly the appearance you want.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by