Write NaN to file when exporting data


Is it possible to write NaN to a file when exporting data using the Save or SaveTableCopy operations rather than leaving the field blank for example when writing to a csv file? If not, is that a possible flag to add? 

I have no quick code to test this, but I think you should get NaN if you convert the data to text before saving. This could be achieved easily in a script, where all waves from the selected table are collectively written into a temporary text wave before invoking the save command. What do you want to do with the data once it's written? I believe NaN has a quite strict definition and just writing the text 'NaN' in a csv file, for example, serves only a very limited purpose (namely that you can read it in a text editor).

Hi Kaikin

You can do this with wfprintf also, like in the example below. AFAIK wfprintf can take several (many) waves as input to make a table. At least in Igor 9 wfprintf is also very fast even for large datasets too.

Function wrNaN(String fNameStr)

    double refNum
    String output
    Make /FREE junk = NaN
    wfprintf output, "%.f,", junk
    Open refNum as fnamestr
    FBinWrite refNum, output
    Close refNum


Thanks, both good suggestions and worked just fine. I've just run into a couple of cases where a colleague who sadly didn't use Igor needed to read a file that I was exporting and requested actual NaNs.

You can write a routine that replaces ",," with ",NaN," and replaces ",<terminator>" with ",NaN<terminator>".

Here is some untested code that is based on this code snippet:

Function ChangeBlanksToNaNsInFile(pathName, fileName)
    String pathName     // Name of an Igor symbolic path or ""
    String fileName         // Name of file or full path to file
    Variable refNum
    // Open source file and read the raw text from it into a string variable
    Open/Z=1/P=$pathName refNum as fileName
    if (V_flag != 0)
        return -1                       // pathName and fileName do not fully specify the file to be opened

    FStatus refNum                      // Sets V_logEOF
    Variable numBytesInFile = V_logEOF
    String text = PadString("", numBytesInFile, 0x20)
    FBinRead refNum, text               // Read entire file into variable.
    text = ReplaceString(",,", text, ",NaN,")
    text = ReplaceString(",\r", text, ",NaN\r") // For files that use CR or CRLF terminators
    text = ReplaceString(",\f", text, ",NaN\f") // For files that use LF terminators
    FSetPos refNum, 0                   // Write string back to file
    FBinWrite refNum, text
    Close refNum
    return 0


Or run a (text editor) REGEX function on the CSV text file to replace strings ",,", ",\r" and ",\f" with ",NaN,". :-)

If you want to export or write a numeric NaN, you could try one of the IEEE bit-wise representations.

A simple solution in Igor Pro is to assign the numeric value 0/0. From the command line,

print 0/0

gives NaN.