MATLAB: Does the TEXTSCAN function with the ‘treatAsEmpty’ option using ‘NAN’ show an in MATLAB 7.3 (R2006b)

MATLABnantextscantreatasempty

When I use the following code:
textscan(fid,'%s%s%f%f%f%f','delimiter','|','treatAsEmpty','NAN')
MATLAB shows the following error:
??? Error using ==> textscan
TreatAsEmpty strings cannot be numeric.

Best Answer

This is expected behavior in MATLAB. The 'treatAsEmpty' option accepts only string inputs. When the input is of the form 'NAN' or any combination {nan,NAN}, they are treated as NaN(Not a Number). Since NaN has numerical significance, it throws the error “TreatAsEmpty strings cannot be numeric.”
The workaround is to replace all fields that are NAN with NaN and call the textscan function without the 'treatAsEmpty' option, for Example:
textscan(fid,'%s%s%f%f%f%f','delimiter','|');