简体   繁体   English

如何从 a.csv 文件中读取带有 readtable 的确切小数位数?

[英]How read the exact number of decimal digits with readtable from a .csv file?

I want to read a.csv file containing numbers with more than 17 decimal digits by using the function readtable.我想使用 function 可读表读取包含十进制数超过 17 位的数字的 a.csv 文件。 I manage to perform this task but the collected data has a reduced number of decimal digits with respect to data stored into orginal.csv file.我设法执行此任务,但与存储到 orginal.csv 文件中的数据相比,收集的数据的十进制位数减少了。

How can I keep the same number of decimal digits as in the original file in matlab?如何在 matlab 中保持与原始文件相同的十进制位数?

Here an example code that tries to read the.csv file "NEOs_asteroids.csv" attached to this question:这是一个示例代码,它试图读取附加到这个问题的.csv 文件“NEOs_asteroids.csv”:

clear all; close all; clc;

file_name_asteroids = 'NEOs_asteroids.csv';

% Options settings 
opts = detectImportOptions(file_name_asteroids);
opts.DataLines = 2;
opts.VariableNamesLine = 1;

% Read data into a table array
Asteroids_orbit_elem = readtable(file_name_asteroids,opts);

Here the.csv file "NEOs_asteroids.csv" (I have only inserted a few lines because of the excessive number of them):这里是.csv文件“NEOs_asteroids.csv”(由于数量过多,我只插入了几行):

"pdes","name","epoch","a","e","i","om","w","ma"
433,Eros,2459600.5,1.458273100364966,.2227273342470533,10.82846064209305,304.2963534821116,178.8971619902451,246.9041236220357
719,Albert,2459600.5,2.63750535267235,.5469586853330971,11.57526970278196,183.8552591536149,156.2275765643465,278.1971857577304
887,Alinda,2459600.5,2.473158049128383,.5704858457447907,9.394142747629626,110.4288007693465,350.4932417946612,86.60720939790032
1036,Ganymed,2459600.5,2.665849369179047,.5331216208605787,26.6779034163866,215.5172093946143,132.4281002671606,140.6548420252212
1221,Amor,2459600.5,1.918731194899893,.4358425037702864,11.88326264445768,171.320253219285,26.64392069638918,261.0445055510584
1566,Icarus,2459600.5,1.078092896249167,.8269435773988872,22.80421609750081,87.96160578064956,31.43750761745641,.3302900481594904
1580,Betulia,2459600.5,2.197288160959855,.4871550690589618,52.07925829047207,62.2921305585828,159.5134530240992,16.62891169416756
1620,Geographos,2459600.5,1.245689288382424,.3353623815747189,13.33753650197932,337.1849253154281,276.9463790257625,300.4925456677647
1627,Ivar,2459600.5,1.863273429711433,.3967369932429542,8.451956515515922,133.1203083485335,167.8035837515731,129.0289785118674
1685,Toro,2459600.5,1.367465568905196,.435966790549019,9.382739662870495,274.2362441040706,127.2114688956593,300.0617970060148
1862,Apollo,2459600.5,1.470502475713431,.5600430467767759,6.353083928279622,35.56898244993719,286.0386669340027,60.20237527657879
1863,Antinous,2459600.5,2.25922922966352,.6059920366929147,18.39886422664638,346.4237098245764,268.0896471241496,244.0101430178307
1864,Daedalus,2459600.5,1.460979692497152,.614486601048712,22.21536436924144,6.613194566464952,325.6563726322316,196.1166696209867
1865,Cerberus,2459600.5,1.079986459127503,.4669721847798676,16.09858904525619,212.8963846822369,325.2653260753237,170.2073771841984
1866,Sisyphus,2459600.5,1.893353802479725,.5383695942642949,41.20637430558688,63.47259682927852,293.0824330786623,331.1476239661798
1915,Quetzalcoatl,2459600.5,2.543790754476287,.5706410666538692,20.40472660837776,162.9348721741076,347.8091351438147,41.39145147165023
1916,Boreas,2459600.5,2.272324177667199,.4498393843780145,12.88297395642586,340.5981620510102,335.9167764123868,352.2401366045371
1917,Cuyo,2459600.5,2.149811212264342,.5054666607934076,23.95922692432141,188.2869783183211,194.5115030152427,82.28439227062766
1943,Anteros,2459600.5,1.430366783173011,.2560374828122278,8.706034969532251,246.3110706020363,338.3801329232052,173.8814283992249

It is likely that you are running into a precision limitation of the floating point format used internally by MATLAB.您可能遇到了 MATLAB 内部使用的浮点格式的精度限制。 MATLAB by default uses doubles to store pretty much all numbers. MATLAB 默认使用双精度数来存储几乎所有数字。 For an IEEE double you're only going to get about 15 decimal digits .对于 IEEE 双精度数,您只会得到大约15 个十进制数字

If you're not planning on performing computations on these numbers an option is to read them in as strings :如果您不打算对这些数字执行计算,则可以选择将它们作为字符串读入

opts = detectImportOptions(filename);
opts = setvartype(opts, 'your variable', 'string');  % or 'char'
data = readtable(filename, opts);

If you want to perform computations on these numbers things are somewhat more difficult.如果你想对这些数字进行计算,事情就有点困难了。 A "built-in" option is to use the variable-precision arithmetic in the Symbolic Math Toolbox, or roll you own implementation. “内置”选项是使用符号数学工具箱中的可变精度算术,或者滚动您自己的实现。 I'd consider if you really need the precision first before going down either of these paths for more precision.我会考虑你是否真的需要精度,然后再沿着这些路径中的任何一条获得更高的精度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM