Which character conversion notation do I have to use?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ben van Zon
le 13 Sep 2023
Modifié(e) : Ben van Zon
le 14 Sep 2023
I am currently working with a big 1610x19 .txt file from which I wanna gather information about specific rovibrational transitions. But first I want everything in a working table file, but it keeps giving random errors like:
Error using readtable
Unable to read the entire file. You may need to specify a different format, delimiter, or number of header lines.
Error in MeasuredTransitions (line 6)
Lines = readtable(FileName, 'Format', '%.3f %s %c %c %c %d %c %d %s %s %s %s %s %s %s %s %s %s', 'HeaderLines', 18, 'ReadVariableNames', true, 'Delimiter', '\t');
Caused by:
Reading failed at line 20. A field on that line may have contained the wrong type of value.
Even though the file itself isn't any different at the line in question.
Now, what characters do I have to use so I can use all the text and values that are in the data?
The data in question is attached.
2 commentaires
Dyuman Joshi
le 13 Sep 2023
You have 19 columns and 18 format specifications.
Any particular reason why you are reading numeric data as string? Why not just use readtable() directly?
Stephen23
le 13 Sep 2023
The file contains no header lines:

Question: why do you specify 18 header lines?
Réponse acceptée
Stephen23
le 13 Sep 2023
Modifié(e) : Stephen23
le 13 Sep 2023
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string')
3 commentaires
Stephen23
le 14 Sep 2023
Modifié(e) : Stephen23
le 14 Sep 2023
"I tried running it like that but at the last rows it starts returning NaNs for me."
Aaah, I can see that all of the columns can contain non-numeric data, except for the first two columns. In particular, those columns contain the characters 'l', 'm', and 'u' (and perhaps others) as this screenshot shows:

READTABLE has converted that non-numeric data in numeric columns to NaN, which seems reasonable.
If those non-numeric data need to be retained AND you want to retain the numeric data then perhaps READCELL:
C = readcell('ct4004355_si_003.txt', 'Delimiter','\t')
C(end-8:end,:)
Not very easy to use, but there are your m's together with some numeric data.
Otherwise specify the variable class before calling READTABLE:
fnm = 'ct4004355_si_003.txt';
obj = detectImportOptions(fnm, 'Delimiter','\t');
obj = setvartype(obj,'string');
obj = setvartype(obj,1:2,'double');
T = readtable(fnm,obj)
T(end-8:end,:)
Well, there are those m's again, but now everything is text. Which do you prefer?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!