MATLAB: Does the FSCANF function in MATLAB not leave the file pointer at the correct position when the format specifier includes trailing “skip” items

MATLAB

I have a text file that contains the following:
a;b;c;d;
I use the FSCANF function to read the "a" and "b", using a "skip" format to skip the semicolons:
out = fscanf(fid, '%c%*c', 2);
where "fid" is the file identifier of the file.
However, the file pointer, as returned by
ftell(fid)
is left at 3, just ahead of the second semicolon. I expected the file pointer to be left at 4, just after the second semicolon.

Best Answer

This is expected behavior: In the final repetition of the format string, FSCANF will stop reading after it has read the last unstarred (i.e. not skipped) format specifier or literal character in format string.
To force the file pointer to advance until the end of the format string, you can either:
1. Use literal characters to read the trailing non-captured items:
out = fscanf(fid, '%c;', 2);
2. Capture these items in the output, and programmatically manipulate the results to remove them
out = fscanf(fid, '%c%c', 4);
out = out(1:2:end);