How read the exact number of decimal digits with readtable from a .csv file?
이전 댓글 표시
Hi guys,
I want to read a .csv file containing numbers with more than 17 decimal digits (I'm not sure of the exact number of these decimal digits) by using the function readtable.
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.
How can I keep the same number of decimal digits as in the original file in matlab?
Here an example code that tries to read the .csv file "NEOs_asteroids.csv" attached to this question.
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);
댓글 수: 9
"I want to read a .csv file containing numbers with more than 17 decimal digits..."
In fact your file data contains maximum 16 significant digits:
str = fileread('NEOs_asteroids.csv');
raw = regexp(str,'\d*\.?\d+','match');
len = cellfun(@numel,regexprep(raw,'(^0?\.0+|\.)',''));
[mxl,idx] = max(len)
raw{idx}
"How can I keep the same number of decimal digits as in the original file in matlab?"
Whilst importing the file data as text is certainly possible and will retain all of the digits as you request, it will make processing your data afterwards slow and complex. What is much more likely is that you are misunderstand the significance (in the mathematical sense) of those digits and what they represent in DOUBLE values.
Lets take a look at that number:
format long G
num = str2double(raw{idx})
fprintf('%.42f\n',num)
fprintf('%.16g\n',num)
Those values were (most likely) originally saved from DOUBLE values. MATLAB imports those values as the closest DOUBLE values, which perfectly match the DOUBLE values used to create the file. Lets try converting those strings to double and then back again, to see if we lose any information:
vec = str2double(raw);
isequal(vec,str2double(compose("%.16g",vec))) % round-trip conversion
Yep, exactly the same (just as expected). So what you are trying to do is based on a misunderstanding of the significance of DOUBLE values, and will be a lot of effort for absolutely zero benefit.
Giuseppe
2022년 1월 30일
WRITETABLE writes text files using FORMAT LONG G, so it has 15 significant digits:
If you must maintain exactly the same information as the original file then you can import the file data as text.
Giuseppe
2022년 2월 2일
Giuseppe
2022년 2월 2일
Stephen23
2022년 2월 2일
"How can I link the filtered numerical data to the original data?"
That is exactly what indexing is for. See my answer.
채택된 답변
추가 답변 (1개)
David Hill
2022년 1월 30일
The digits should still be there (just not displayed) as long as they do not exceed floating point accurracy. Look at this:
file_name_asteroids = 'NEOs_asteroids.csv';
opts = detectImportOptions(file_name_asteroids);
opts.DataLines = 2;
opts.VariableNamesLine = 1;
Asteroids_orbit_elem = readtable(file_name_asteroids,opts);
format long
Asteroids_orbit_elem(1:10,:)
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!