Which character conversion notation do I have to use?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Dyuman Joshi
2023-9-13
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
2023-9-13
编辑:Stephen23
2023-9-13
T = readtable('ct4004355_si_003.txt', 'Delimiter','\t', 'TextType','string')
3 个评论
Stephen23
2023-9-14
编辑:Stephen23
2023-9-14
"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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!