Which character conversion notation do I have to use?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ben van Zon
el 13 de Sept. de 2023
Editada: Ben van Zon
el 14 de Sept. de 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 comentarios
Dyuman Joshi
el 13 de Sept. de 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
el 13 de Sept. de 2023
The file contains no header lines:

Question: why do you specify 18 header lines?
Respuesta aceptada
Stephen23
el 13 de Sept. de 2023
Editada: Stephen23
el 13 de Sept. de 2023
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string')
3 comentarios
Stephen23
el 14 de Sept. de 2023
Editada: Stephen23
el 14 de Sept. de 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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!