disp gives wrong format

2 visualizzazioni (ultimi 30 giorni)
Kelly McGuire
Kelly McGuire il 17 Ott 2019
Risposto: Image Analyst il 17 Ott 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

Risposte (2)

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

Accedi per commentare.


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

Community Treasure Hunt

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

Start Hunting!

Translated by