Save Waves as Comma-Delimited Text

// SaveWavesDelimited(pathName, filePath, list, delimiter, includeWaveNames, overwriteMode)
// As of Igor Pro 6.22, the Igor Save operation can not save comma-delimited files.
// It can save tab-delimited (/J) or space-delimited (/G).
// To work around this, this routine writes the wave data and then replaces the
// tab delimiter in the saved file with the specified delimiter.
// The target file is specified using an Igor symbolic path and a file name, partial path
// to the file or full path to the file. Pass "" for filePath to get a Save file dialog.
// If you are not familiar with Igor symbolic paths, execute this and read the help:
//  DisplayHelpTopic "Symbolic Paths"
// The function returns 0 if successful or non-zero if an error occurred or the user cancelled.
// Read the comments for the parameters for further information.
Function SaveWavesDelimited(pathName, filePath, list, delimiter, includeWaveNames, overwriteMode)
    String pathName         // Igor symbolic path name or "" if filePath is a full path.
    String filePath             // File name, partial path or full path. "" to get save file dialog.
    String list                 // Semicolon-separated list of wave names.
    String delimiter                // e.g, "," for comma-delimited.
    Variable includeWaveNames   // 1 to include wave names as column labels.
    Variable overwriteMode      // 0 to never overwrite, 1 to always overwrite, 2 to display save file dialog if file already exists.
   
    // These are used if a Save File dialog is displayed
    String fileFilter = "All Files:.*;"
    String prompt = "Save waves as delimited text"
   
    Variable refNum
   
    GetFileFolderInfo /P=$pathName /Q /Z filePath
    if (V_Flag == 0)
        // File or folder exists
        if (V_isFolder)
            Printf "Can not overwrite folder at \"%s\"\r", S_Path
            return -1
        endif
        // File exists
        switch (overwriteMode)
            case 0:             // Never overwrite
                Printf "File \"%s\" already exists\r", S_Path
                return -1
                break
            case 1:             // Always overwrite
                filePath = S_Path
                break
            case 2:             // Display Save File dialog
                Open /P=$pathName /D /F=fileFilter /M=prompt refNum as filePath
                filePath = S_fileName           // Full path to the file
                if (strlen(filePath) == 0)
                    return -1                   // User cancelled
                endif
                break
        endswitch
    else
        // File does not exist. See if we can create it.
        Open /P=$pathName /Z refNum as filePath
        if (V_flag == 0)
            // The file was created.
            filePath = S_fileName           // Full path to the file
            Close refNum
        else
            // Display a save file dialog
            Open /P=$pathName /D /F=fileFilter /M=prompt refNum as filePath
            filePath = S_fileName           // Full path to the file
            if (strlen(filePath) == 0)
                return -1                   // User cancelled
            endif
        endif
    endif

    // The overwrite parameter was handled above so we can always overwrite here
    if (includeWaveNames)
        Save /J /B /W /O list as filePath
    else
        Save /J /B /O list as filePath
    endif
   
    // Now replace the tab delimiter with the specified delimiter
   
    // Open file and read data
    Open /R refNum as filePath
    FStatus refNum
    Variable numBytes = V_logEOF
    String text = PadString("", numBytes, 0x20 )        // FBinRead requires this
    FBinRead refNum, text
    Close refNum
   
    text = ReplaceString("\t", text, delimiter)
   
    // Rewrite file
    Open refNum as filePath
    FBinWrite refNum, text
    Close refNum
   
    return 0   
End

I suspect Mac users could make use of an ExecuteScript shell script call properly stated using grep to replace the tabs in the first-written file with commas and avoid doing a file re-open, change, and rewrite from within Igor. Something along the lines of ...

// Now replace the tab delimiter ...

#if Windows
  Open/R refNum as filePath
  ...
  Close refNum
#else
  string theCmd = "grep ... %s" filePath
  ExecuteScript theCmd
#endif


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More