Which character conversion notation do I have to use?
1 view (last 30 days)
Show older comments
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 Comments
Dyuman Joshi
on 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
on 13 Sep 2023
The file contains no header lines:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1481001/image.png)
Question: why do you specify 18 header lines?
Accepted Answer
Stephen23
on 13 Sep 2023
Edited: Stephen23
on 13 Sep 2023
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string')
3 Comments
Stephen23
on 14 Sep 2023
Edited: Stephen23
on 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482396/image.png)
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?
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!