How do I prevent MATLAB from converting a number into scientific notation?
140 views (last 30 days)
Show older comments
I am trying to make a plot from two columns in a data table on Matlab. However, Matlab converts some of my values in the data table into scientific notation, which I don't want? For example, I have a column for date and time that uses codes like 1910011037 (October 1st, 2019, 10:37 AM). But matlab rounds this and applies scientific notation so that the value in my date/time column looks like this 1.9100e+09.
Is there anyway for me to fix this? Thank you in advance!
0 Comments
Answers (1)
madhan ravi
on 15 Jun 2020
format longg
3 Comments
John D'Errico
on 15 Jun 2020
Edited: John D'Errico
on 15 Jun 2020
Note that MATLAB ALWAYS stores all numbers in a binary form with an exponent. This is an IEEE standard, unless you use an integer format to store the numbers, such as int64, etc.
Your choice is really only how to display the numbers. That is controlled by the format command.
Sometimes people become confused, when an array has numbers that vary over many orders of magnitude. In that case, even if the numbers happen to be purely integer, you may see scientific notation used.
format short g
>> X = 2.^reshape(1:40,10,4)
X =
2 2048 2.0972e+06 2.1475e+09
4 4096 4.1943e+06 4.295e+09
8 8192 8.3886e+06 8.5899e+09
16 16384 1.6777e+07 1.718e+10
32 32768 3.3554e+07 3.436e+10
64 65536 6.7109e+07 6.8719e+10
128 1.3107e+05 1.3422e+08 1.3744e+11
256 2.6214e+05 2.6844e+08 2.7488e+11
512 5.2429e+05 5.3687e+08 5.4976e+11
1024 1.0486e+06 1.0737e+09 1.0995e+12
As it turns out, every one of those numbers will be represented exactly as integers, even though the command window uses scientific notation to store them.
Use of a different choice for the format command would have worked, because as long as the integers do not exceed 2^53-1, they will be exactly representable by a double. As well, I could have forced MATLAB to use int64 here.
X = int64(2).^reshape(int64(1:40),10,4)
X =
10×4 int64 matrix
2 2048 2097152 2147483648
4 4096 4194304 4294967296
8 8192 8388608 8589934592
16 16384 16777216 17179869184
32 32768 33554432 34359738368
64 65536 67108864 68719476736
128 131072 134217728 137438953472
256 262144 268435456 274877906944
512 524288 536870912 549755813888
1024 1048576 1073741824 1099511627776
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!