How to read non-printing characters(tab and crlf)?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pankaj
el 2 de En. de 2015
Comentada: Star Strider
el 2 de En. de 2015
Hello all,
I have ill-formatted data in a text file, the values are separated by either tab or space and if the values are missing then nothing is saved(no garbage value). Following is an example of data format:
2011-06-16 19:45 \t 20.5 \t 18.7 \t 0.6 2.7
2011-06-16 20:00 \t\t 18.7 \t 0.6 \t 2.7
where \t represents a horizontal tab. Every time I have to check if the character is tab, whitespace or a value. I tried DLMREAD or TEXTSCAN but they do not solve my purpose. I have also attached a sample data file.
Thanks
0 comentarios
Respuesta aceptada
Star Strider
el 2 de En. de 2015
It took some experimenting, but I can read your ‘Sample Data.txt’ file with this:
fidi = fopen('Pankaj Sample Data.txt');
d = textscan(fidi, '%10s %5s %f %f %f %f', 'Delimiter','\t', 'EndOfLine','\r\n', 'EmptyValue',NaN, 'CollectOutput',1);
d1 = d{1}; % Check Result
d2 = d{2}; % Check Result
The first 10 rows for both are:
d1 =
'2011-06-16' '17:00'
'2011-06-16' '17:15'
'2011-06-16' '17:30'
'2011-06-16' '17:45'
'2011-06-16' '18:00'
'2011-06-16' '18:15'
'2011-06-16' '18:30'
'2011-06-16' '18:45'
'2011-06-16' '19:00'
'2011-06-16' '19:15'
d2 =
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
20.1 17.4 0.3 2.6
19.6 NaN 0.2 2.6
20.1 17.9 0.3 2.7
20.5 NaN 0.6 2.7
NaN 18.7 0.6 2.7
NaN 18.7 0.6 2.7
2 comentarios
Star Strider
el 2 de En. de 2015
My pleasure!
I learned a bit more about textscan as well.
You can scan workspace variables using textscan. See specifically this section of the textscan documentation for details.
Más respuestas (0)
Ver también
Categorías
Más información sobre Text Data Preparation 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!