Prevent output of Exponential Notation in For-Loop

4 visualizaciones (últimos 30 días)
Robert
Robert el 28 de Mzo. de 2019
Respondida: Walter Roberson el 28 de Mzo. de 2019
I know this has been asked many, many, many, many, many times on this forumn, but with every answer and alteration I could find to the format and fprintf functions possible, I have been unable to solve my problem.
I am running a for-loop to convert the Year, Month and Day columns of a 3128x10 matrix into a fraction of the year, formatted as YYYY.%%, as the year with the fraction of the year passed added to create a decimal value. My current code is as follows:
for i = 1:3128
StartDate = ['01/01/' num2str(data(i,1))]; % set StartDate as 01-Jan of the year of the selected row, `i`
EndDate = [num2str(data(i,2)) '/' num2str(data(i,3)) '/' num2str(data(i,1))]; % format the EndDate as MM/DD/YYYY
data(i,11) = data(i,1) + yearfrac(StartDate,EndDate,10); % determine fraction of year passed and add to the current year
end
I then add the output value to the original matrix as the 11th column. The problem is that when I run this, the 11th column is set as rounded exponential notation, often rounded incorrectly to make disorder in the chronological order of events, making the data impossible to plot.
Like I said, I have applied format longg, format shortg, and sprintf() in the for-loop and before the for-loop in my script and both have been to no use so far.
Any assistance would be appreciated.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Mzo. de 2019
There is no way of doing what you want in MATLAB, other than creating a complete replacement numeric class that has different internal formatting.
The problem is (probably) not the way that the data is stored but rather the way it is displayed. None of the numeric formats that can be controlled by using the format command will output full precision, and none of them can have their output precision fine controlled.
If all you need to do is display the values (or store them to file) then you can create your own fprintf() statement that uses an appropriate precision for each column.
When you talk about making data impossible to plot, then I suspect you are copying the output of displaying, and pasting it in elsewhere, instead of passing the data itself or using binary save and load.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by