disp gives wrong format

2 次查看(过去 30 天)
Kelly McGuire
Kelly McGuire 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 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by