Problem reading lines from .dat file

조회 수: 2 (최근 30일)
Denis Wolff
Denis Wolff 2018년 5월 16일
답변: Denis Wolff 2018년 5월 16일
Hello,
I want to read a .dat file into matlab line by line, using a function like 'textscan' or 'sscanf'. Each line looks like the following:
3,424, 10:50:34.177, 0, 2,863333, 1,478, 54, 127,7047, 90,19555, 0, 0, 0,7567907, 3479,262, 16,07539, 0, 108, 228, 48, 1,046861, 1,067549, 0, 1,096976, 123, 88, 1613,094, 56449,5, 6, 0,5, 103,0865, 0
What is inconvenient is that the commas (',') are used both as delimiters and for the decimal places. Apparently this causes an error when reading the line, for instance when I'm using
textscan(tline,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','Delimiter',', ','EmptyValue',-Inf);
it seperates the "3,424" into "3" and "424". I tried using different options for Delimiter , as ', ' or ',\b' to make clear that the numbers are seperated by a Comma+Empty Space, but I couldn't find any way to tell Matlab that there are two types of commas and that it shoud seperate '3,424' from '10:50:34.177' but not '3' from '424'.
Can anyone please help me correctly reading these lines into Matlab?
Many thanks, Denis

채택된 답변

Jan
Jan 2018년 5월 16일
편집: Jan 2018년 5월 16일
Using commas as decimal point and separator of the data is not only "inconvenient", but a disaster. The trustworthy solution is to delete the files and recreate them with a reliable dot.
There is one chance, that the commas used as separators are followed by a space in every case. Is this true? Then you can fix the files:
S = fileread(FileName);
S = strrep(S, ', ', '<magic!>');
S = strrep(S, ',', '.');
S = strrep(S, '<magic!>', ', ');
fid = fopen(FileName, 'w');
fwrite(fid, S, 'char');
fclose(fid);
By the way, I'd use '%s' or even better '%D' to import '10:50:34.177' .

추가 답변 (1개)

Denis Wolff
Denis Wolff 2018년 5월 16일
Great, that's the solution! I could fix the files (it wasn't my choice to respresent them this way, but fortunately the commas are followed by a space in every case).
Thank you very much!

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by