Why doesn't the "textscan" function display the empty character after the delimiter?

I found that the "textscan" function returns a different number of tokens than expected when a string ends with a delimiter. For example:
'a,b,c' --> 3 tokens
'a,,c' --> 3 tokens
'a,b,' --> 2 tokens, but expected 3 tokens
I would expect n+1 tokens for n delimiters assuming multiple delimiters are not being combined together.
Is this expected behavior? And is there a way to display the empty character at the end after the delimiter?

 Accepted Answer

This is the expected behavior. When textscan reaches the end of an array (or file), it only returns data for the specifiers which actually had data:
'a,b,c' % parses as |a,|b,|c|<end>
'a,,c' % parses as |a,|,|c|<end>
'a,b,' % parses as |a,|b,|<end>
Since there's only one specifier, each time it consumes a delimiter, it considers the next piece of data as the start of a new line. So, given the input in "textscan":
'a,b,c' is equivalent to sprintf('a\nb\nc\n')
'a,b,' is equivalent to sprintf('a\nb\n')
As a workaround, concatenate the string with another delimiter that would make "textscan" treat the empty trailing element as a field, and not as the end of the string.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!