Read mixed formate Data from Text Files

조회 수: 2 (최근 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 CenterFile Exchange에서 Large 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