Problem reading lines from .dat file

1 view (last 30 days)
Denis Wolff
Denis Wolff on 16 May 2018
Answered: Denis Wolff on 16 May 2018
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

Accepted Answer

Jan
Jan on 16 May 2018
Edited: Jan on 16 May 2018
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' .

More Answers (1)

Denis Wolff
Denis Wolff on 16 May 2018
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!

Community Treasure Hunt

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

Start Hunting!