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 日
The file contains no header lines:

Question: why do you specify 18 header lines?
採用された回答
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:

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 で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!