How to use a two character string as a delimiter in "textscan" function?

I want to use " 'h " as a single delimiter to extract the letters after " 'h " with "textscan" function. I can use " h " as a delimiter, but I cannot use " 'h " as a single delimiter.

 Accepted Answer

For large files with specific delimiter:
You can achieve this using the "MultipleDelimsAsOne" property in 'textscan'.
For example, 
C = textscan(fileID,'%s %s','Delimiter',{'''h'},'MultipleDelimsAsOne',1,'TreatAsEmpty',{'NA','na'},'CommentStyle','//');
For more details, you can access the release-specific documentation by executing the following command in the MATLAB R2020b command window:
>> web(fullfile(docroot, 'matlab/ref/textscan.html?searchHighlight=textscan'))
For smaller text files:
You can use "regexp" instead, as there might be an issue with the order of delimiters.
file = fileread(filename);
pieces = regexp(file, "'h", 'split');
if isempty(pieces{end})
pieces(end) = [];
end %special condition for EOF

1 Comment

Note that if you do this, then it will not pay attention to order, and it will treat singles as delimiters. For example
'hello henry, it is 3 o'clock
DD D D
each location marked as D would be treated as a delimiter
You cannot reliably create two-character-pair delimiters with textscan. You can reduce the problem by using something like 'delimiter', '''', with format "h%[^']" but that would get you to
'hello henry, it is 3 o'clock
Dh.....................DE
That is, Delimiter, then the literal h (that would not appear in the output stream, then all of the character positions marked with period would be included in the output. Then the ' delimiter would be found, ending the %[^'] . And at that point you would have a problem as the "c" does not match the expected literal h
Because of this, unless you have really big files, use a different approach:
S = fileread('NameOfFile');
pieces = regexp(S, "'h", 'split');
if isempty(pieces{end}); pieces(end) = []; end %special condition for EOF

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!