How read the exact number of decimal digits with readtable from a .csv file?
Afficher commentaires plus anciens
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 commentaires
"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
le 30 Jan 2022
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
le 2 Fév 2022
Giuseppe
le 2 Fév 2022
Stephen23
le 2 Fév 2022
"How can I link the filtered numerical data to the original data?"
That is exactly what indexing is for. See my answer.
Réponse acceptée
Plus de réponses (1)
David Hill
le 30 Jan 2022
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,:)
1 commentaire
Giuseppe
le 2 Fév 2022
Catégories
En savoir plus sur Text Files dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!