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

9 views (last 30 days)
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.

Answers (1)

Stephen23
Stephen23 on 22 Aug 2016
Edited: Stephen23 on 22 Aug 2016
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 Comment
Guillaume
Guillaume on 22 Aug 2016
Edited: Guillaume on 22 Aug 2016
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 %

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!