Problem reading lines from .dat file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Denis Wolff
le 16 Mai 2018
Réponse apportée : Denis Wolff
le 16 Mai 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
0 commentaires
Réponse acceptée
Jan
le 16 Mai 2018
Modifié(e) : Jan
le 16 Mai 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' .
0 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!