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

65 visualizaciones (últimos 30 días)
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 comentarios
Giuseppe
Giuseppe el 2 de Feb. de 2022
How can I link the filtered numerical data to the original data?
Stephen23
Stephen23 el 2 de Feb. de 2022
"How can I link the filtered numerical data to the original data?"
That is exactly what indexing is for. See my answer.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 2 de Feb. de 2022
Editada: Stephen23 el 2 de Feb. de 2022
This imports the data as text, converts to numeric for the filter calculation, and then saves the text. Caveat: it does not preserve the double quotes, which are removed automatically by READTABLE.
i_max = 5; % (deg)
e_max = 0.1;
q_min = 0.9; %(AU)
ad_max = 1.1; % (AU)
fnm = 'NEOs_asteroids.csv';
opt = detectImportOptions(fnm);
opt = setvaropts(opt,'Type','string');
tbs = readtable(fnm, opt); % <- table of text data!
tbn = array2table(str2double(tbs{:,:}),...
'VariableNames',tbs.Properties.VariableNames); % <- table of numeric data!
idx = tbn.i<=i_max & tbn.e<=e_max & tbn.q>=q_min & tbn.ad<=ad_max;
writetable(tbs(idx,:),'NEOs_asteroids_filtered.csv')
And if you want to filter those tables (e.g. for further processing in MATLAB) then of course you can trivially use exactly the same index:
tbs_filtered = tbs(idx,:) % <- filtered text table!
tbn_filtered = tbn(idx,:) % <- filtered numeric table!
  3 comentarios
Giuseppe
Giuseppe el 2 de Feb. de 2022
Oh, thanks very much! Can you also show me a short procedure to fill the empty the fields of column "name" with the string"(NO NAME)" within the strings variable?
Stephen23
Stephen23 el 3 de Feb. de 2022
"...fill the empty the fields of column "name" with the string"(NO NAME)" within the strings variable?"
ide = ismissing(tbs.name);
tbs.name(ide) = "(NO NAME)";

Iniciar sesión para comentar.

Más respuestas (1)

David Hill
David Hill el 30 de En. de 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 comentario
Giuseppe
Giuseppe el 2 de Feb. de 2022
@David Hill thank for your answer. As I wrote in the above answer, I lose some digits when exporting to a new file. Can you check and give me a feedback?

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by