Read mixed formate Data from Text Files

1 Ansicht (letzte 30 Tage)
WB
WB am 20 Apr. 2016
Bearbeitet: Walter Roberson am 20 Apr. 2016
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

Antworten (1)

Meade
Meade am 20 Apr. 2016
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 Kommentare
WB
WB am 20 Apr. 2016
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 am 20 Apr. 2016
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);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by