필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Pankaj
Pankaj 2015년 1월 2일
댓글: Star Strider 2015년 1월 2일
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

채택된 답변

Star Strider
Star Strider 2015년 1월 2일
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
Pankaj
Pankaj 2015년 1월 2일
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 2015년 1월 2일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by