How to read non-printing characters(tab and crlf)?

6 visualizaciones (últimos 30 días)
Pankaj
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

Respuesta aceptada

Star Strider
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
Pankaj
Pankaj el 2 de En. de 2015
Thank you Star, that was of great help to me. I spent many hours in figuring it out but I couldn't. I have one more doubt: Can we scan a char type variable which is in workspace, like we scan a text file. I searched methods to open it as we open text file(fopen) but again could't find it. I am attaching a sample file for reference.
Thank you very much again :)
Star Strider
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.

Iniciar sesión para comentar.

Más respuestas (0)

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!

Translated by