MATLAB: Does TEXTSCAN read until the end of the file when I specify a variable number of cycles

datadoubleendeoffileintegerMATLABoftextscantypetypecast

If I read from a file using the following code, MATLAB reads until the end of the file instead of the number of cycles specified:
fid = fopen('test.txt','r');
N = int32(3);
C = textscan(fid,'%s',N);

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3). For previous product releases, read below for any possible workarounds:
We have verified that there is a bug in MATLAB 7.0 (R14) that affects the way TEXTSCAN behaves when a variable type other than double is used to specify the number of cycles.
To work around this issue, typecast the variable to a double as in the following example:
fid = fopen('test.txt','r');
N = int32(3);
C = textscan(fid,'%s',double(N));