disp gives wrong format

2 vues (au cours des 30 derniers jours)
Kelly McGuire
Kelly McGuire le 17 Oct 2019
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

Réponses (2)

Wiley Mosley
Wiley Mosley le 17 Oct 2019
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 commentaires
Wiley Mosley
Wiley Mosley le 17 Oct 2019
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 le 17 Oct 2019
I have the 2016b academic version. I put format short before disp(), and it fixed the format.

Connectez-vous pour commenter.


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

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by