MATLAB: Does textscan break when there is a -NaN in the input string or input file

breakbreaksinvalidMATLABminusnannotnumbertextscanwrong

The function textscan breaks when there is a -NaN in the input string or input file. I expect MATLAB to return NaN when a -NaN is in the field (as it does for NaN), but it returns an empty value.
Expected Behavior: The proper result happens when reading a 'NaN' from a string or file:
>> a = textscan('NaN','%f')
a =
[NaN]
An empty value is returned when the string or input file contains '-NaN':
>> a = textscan('-NaN','%f')
a =
[0x1 double]
This cannot be remedied using the 'TreatAsEmpty' option, because MATLAB recognises '-NaN' as a numeric value:
>> a = textscan('-NaN','%f','TreatAsEmpty','-NaN')
??? Error using ==> textscan
TreatAsEmpty strings cannot be numeric.

Best Answer

This enhancement has been incorporated in Release 2010a (R2010a). For previous product releases, read below for any possible workarounds:
This is a limitation in the TEXTSCAN function in MATLAB in the way it handles -NaN. As a workaround read -NaN as a string using TEXTSCAN, remove the '-' sign and convert it back to a number. The attached MATLAB file"removeminusnan" demonstrates this conversion. It accepts a filename (that contains numbers including -NaN) and number of columns of numbers as input and produces a matrix output that contains numbers with -NaN replaced by NaN.
Also attached is a sample text file "Nan.txt" that contains numbers in 5 rows and 4 columns. Call the function "removeminusnan" as below to read the numbers, including -NaN, from the text file:
%Nan.txt is the file that contains 4 columns of numbers
removeminusnan('Nan.txt',4)