How read the exact number of decimal digits with readtable from a .csv file?
63 次查看(过去 30 天)
显示 更早的评论
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 个评论
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.
采纳的回答
Stephen23
2022-2-2
编辑:Stephen23
2022-2-2
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 个评论
Stephen23
2022-2-3
"...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)";
更多回答(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,:)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!