Read mixed formate Data from Text Files
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Meade
el 20 de Abr. de 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 comentarios
Meade
el 20 de Abr. de 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);
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!