How read the exact number of decimal digits with readtable from a .csv file?
66 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Giuseppe
am 29 Jan. 2022
Kommentiert: Stephen23
am 3 Feb. 2022
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
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.
Akzeptierte Antwort
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
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)";
Weitere Antworten (1)
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,:)
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!