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

4 vues (au cours des 30 derniers jours)
Pankaj
Pankaj le 2 Jan 2015
Commenté : Star Strider le 2 Jan 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

Réponse acceptée

Star Strider
Star Strider le 2 Jan 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 commentaires
Pankaj
Pankaj le 2 Jan 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 le 2 Jan 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by