1

My matlab displays the value 1042 as 1.042e+003

How do I get matlab to stop using the e notation and display a 4 digit number ( or longer )

More specifically, this is what I am trying to do :

title(sprintf('1st harmonic of I. Vpeak = %i', AmpHar(i)));

AmpHar(i) has the value 1042. But in the plot title it gets displayed as 1.042e+003

Edit:

of AmpHar(i) has a full value of 1042.3478. But I would like matlab to display the value as 1042.34 in the title

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
sukhvir
  • 5,265
  • 6
  • 41
  • 43

1 Answers1

3

An answer to the edit is, to display the fractional component with the right decimal places, use %f and set the precision:

title(sprintf('1st harmonic of I. Vpeak = %.2f', AmpHar(i)));

Regarding the first question, you are using the correct format specifier for an integer (%i and %d are the same). The problem was that AmpHar(i) is not integer-valued, as it has a fractional component. When MATLAB's sprintf sees this, it switches to exponential notation (%e). Another interesting situation in which it will make this switch to exponential if %d gets an integer larger than 2^63-1. See the solution there.

Community
  • 1
  • 1
chappjc
  • 30,359
  • 6
  • 75
  • 132