How do you specify special character using ASCII code in a textscan format string?

조회 수: 9 (최근 30일)
I have a file that contains special characters (ASCII codes 02 and 03) I would like to parse. The problem is I cannot find how to specify these characters in the textscan format string.
I can load the file with the following code:
data = textscan(fid,'%s','WhiteSpace',char(3),'EndOfLine','')
But I would like to include this character in the format string to directly parse data.
The format string I'd like to write could look like:
data = textscan(fid,'\x02%s\r\nR,%f,%f\r\nL,%f,%f\r\n\x03')
where \x02 and \x03 would specify ASCII codes 2 and 3.
Below is a snapshot of my file in Notepad++:
Thanks for your help.

답변 (1개)

Stephen23
Stephen23 2016년 8월 22일
편집: Stephen23 2016년 8월 22일
The task of encoding non-printing characters by writing printing characters is typically solved by using special characters. In this case though, there no escaped/special characters to represent ASCII chars 2 and 3.
A general solution would be to use sprintf to specify the format string:
>> fmt = sprintf('%s%%s\\r\\nR,%%f,%%f\\r\\nL,%%f,%%f\\r\\n%s',2,3)
fmt =
%s\r\nR,%f,%f\r\nL,%f,%f\r\n
>> +fmt % checking
ans =
2 37 115 92 114 92 110 82 44 37 102 44 37 102 92 114 92 110 76 44 37 102 44 37 102 92 114 92 110 3
Although given the locations of those special characters, you could simply concatenate them onto the ends of the string:
fmt = [char(2),'%s\r\nR,%f,%f\r\nL,%f,%f\r\n',char(3)];
Note if you fopen the file using the t option, then it is not required to specify both \r and \n for each newline as this gets converted to a single newline:
fid = fopen(...,'rt'); % note the "t" !
out = textscan(fid,[char(2),'%s\nR,%f,%f\nL,%f,%f\n',char(3)]) % \r not required
fclose(fid)
This makes your code multi-platform and easier to write. I would highly recommend using the t option when reading text files.
  댓글 수: 1
Guillaume
Guillaume 2016년 8월 22일
편집: Guillaume 2016년 8월 22일
fmt = sprintf('\x02%%s\\r\\nR,%%f,%%f\\r\\nL,%%f,%%f\\r\\n\x03');
would also work. But more readable would be:
fmt = sprintf('\x02%s\x03', '%s\r\nR,%f,%f\r\nL,%f,%f\r\n')
which avoids having to double the \ and %

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

카테고리

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