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

63 Ansichten (letzte 30 Tage)
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 Kommentare
Giuseppe
Giuseppe am 2 Feb. 2022
How can I link the filtered numerical data to the original data?
Stephen23
Stephen23 am 2 Feb. 2022
"How can I link the filtered numerical data to the original data?"
That is exactly what indexing is for. See my answer.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Feb. 2022
Bearbeitet: Stephen23 am 2 Feb. 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 Kommentare
Giuseppe
Giuseppe am 2 Feb. 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 am 3 Feb. 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)";

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Hill
David Hill am 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 Kommentar
Giuseppe
Giuseppe am 2 Feb. 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by