How to read non-printing characters(tab and crlf)?
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 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 Comments
Star Strider
on 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.
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!