MATLAB: How to use a two character string as a delimiter in “textscan” function

MATLAB

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.

Best Answer

For large files with specific delimiter:
You can achieve this using "MultipleDelimsAsOne" property in 'textscan'.
For example, 
C = textscan(fileID,'%s %s','Delimiter',{'''','h'},'MultipleDelimsAsOne',1,'TreatAsEmpty',{'NA','na'},'CommentStyle','//');
Please refer documentation page for more details:
For Smaller text files:
You can use of "regexp" instead since there might be an issue with order of delimiter.
file = fileread(filename);
pieces = regexp(file, "'h", 'split');
if isempty(pieces{end})
pieces(end) = [];
end %special condition for EOF