Read mixed formate Data from Text Files

1 回表示 (過去 30 日間)
WB
WB 2016 年 4 月 20 日
編集済み: Walter Roberson 2016 年 4 月 20 日
Hello--I have multiple text files. The data format in each file is as follows:
Loop n,ID,hh:mm:ss.mmm,data
Example:
Loop 1,2797,13:57:16.073,14,14,16,17,19,20,23,24,27
Loop 2,2793,13:57:16.252,14,15,16,17,19,21,24,25,29,32,35,38,41,45,47
Loop 4,2798,13:57:18.426,14,15,17
Loop 2,2794,13:57:18.607,14,15,16,18,20,21
Loop 3,19086,13:57:18.750,13,14,15,15,16,17,18,19,21,20,21
I have a problem with finding the right formatSpec that separate each line into four cells as follows:
{Loop n} {ID} {hh:mm:ss.mmm} {all the rest data values in the row}
I tried: D = textscan(fileID,'%s %d %d %C [^\n\r]') but it didn't work as expected!
Any idea?
Thanks

回答 (1 件)

Meade
Meade 2016 年 4 月 20 日
Since your data does not have consistent number of columns in each loop, textscan (the whole file at once) may not work.
If you know the maximum number of columns, you can declare that, then change your code as follows:
D = textscan(fileID,'%s %i %D %f %f %f %f %f [^\n\r]','TreatAsEmpty',0)
with "%f" repeated for as many 'data' columns as you think there might be.
If you can't know the maximum number of columns, try fscanf.
  2 件のコメント
WB
WB 2016 年 4 月 20 日
Thanks for your feedback, Meade. Appreciated. In fact, there is no way to know the maximum number of columns. In this case, what would be the formatSpec for fscanf knowing that I only care about the numbers (ignore Loop word)?
Meade
Meade 2016 年 4 月 20 日
Two thoughts: If the number of columns is sufficiently small, you could just guess at the number, say 30. Then delete out the empty columns at the end.
If this is dangerous, you'll probably have to read the file line by line, then parse each line individually. See if this gets you started:
fid = fopen(YOUR_TEXT_FILE);
tline = fgets(fid);
i = 1;
while ischar(tline)
out(i,1) = {tline}
tline = fgetl(fid);
i = i+1;
end
fclose(fid);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by