readtable() Giving NaN for some data files (.txt)

24 vues (au cours des 30 derniers jours)
Andrew Ellingford
Andrew Ellingford le 3 Jan 2020
I have about 6000 .txt data files that I wish to import into Matlab. I have an iterative loop to put these tables into a cell called "Re". I start with a file called Re10000.txt and then Re20000.txt all the way to Re 20 million. This was previously working with an older set of .txt files but as I updated to a newer set of data I am getting some issues which I cannot find the problem of. For some data tables I am getting lots of NaN values. However, I can see that the correct values are being imported, they are just all out of position.
The attched document of Re3750000.txt is one of the text files that is having this problem. I have also attached Re3740000.txt because this text file is working and does not give me any NaN values. These two files are formatted in the same way.
I tried adding the line: T = readtable(fileName, 'TreatAsEmpty',{'.','NA'}); as seen in another post but that did nothing.
When I try to add the data manutally using Home>Import Data it works! But I cannot do that for over 6000 text files
I deleted rows of the text file that does not import correctly and It started working when I deleted all rows after 19. To me, this indicates there may be something with the formatting, but comparing this data file to one that is working, I can not identify anything that would make this import fail.
To try and reproduce this chang "start" to 374000 and "finish" to 375000 and store the files in a folder called "Renamed NACA0018".
start = 10000;
finish = 20000000;
Re = cell(finish/start, 1);
ii = 1;
for i = start:10000:finish
fileName = "Renamed NACA0018\" + "Re" + i + ".txt";
disp(fileName)
T = readtable(fileName);
Re{ii} = T;
ii = ii + 1;
end
i = start;
ii = 1;
  2 commentaires
Stephen23
Stephen23 le 3 Jan 2020
dlmread imports both files without any problems.
Andrew Ellingford
Andrew Ellingford le 7 Jan 2020
That worked, thank you

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 7 Jan 2020
Certainly, it's not clear what is confusing readtable with your second file. Whatever it is it has to do with the space characters between each field.
T = readtable(fileName, 'MultipleDelimsAsOne', true);
fixes the issue.
Or indeed use dlmread. However, note that dlmread is getting deprecated. In newer versions of matlab, it is recommended to use readmatrix but unfortunately, it has the same problem as readtable and doesn't support the 'MultipleDelimAsOne' option. I suppose you could do:
opts = detectImportOptions('Re3750000.txt', 'ConsecutiveDelimitersRule', 'join');
data = readmatrix('Re3750000.txt', opts);
If all the files have the same number of columns, then you only need to do the detectImportOptions on the first file.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by