Read mixed formate Data from Text Files
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!