Load Square Image From 1D Data File

#pragma rtGlobals=3     // Use modern global access method.

// This procedure was written to load a text data file written by Spyglass Transform software.
// The file represented a square matrix but was written with all of the values
// in one column. For example, a matrix like this:
//  1   2   3
//  2   3   4
//  5   6   7
// would be written as:
//  1
//  2
//  ...
//  7
// The function loads the data, redimensions it, and creates an image plot.
// To load a non-square image you will have to modify the procedure.
// See the comments below for further details.

Menu "Load Waves"
    "Load Square Image From 1D Data File...", LoadSquareImageFrom1DDataFile("", "")
End

// LoadSquareImageFrom1DDataFile(pathName, filePath)
// The file is expected to contain a single column of numbers which represent
// an image of nxm dimensions where n==m (i.e., a square matrix).
// This function loads and plots the data. The wave name is based on the file name.
// NOTE: If the wave already exists it is overwritten.
Function LoadSquareImageFrom1DDataFile(pathName, filePath)
    String pathName     // Name of an Igor symbolic path or "".
    String filePath         // Name of file or full path to file.

    // First get a valid reference to a file.
    if ((strlen(pathName)==0) || (strlen(filePath)==0))
        // Display dialog looking for file.
        Variable refNum
        Open/D/R/P=$pathName refNum as filePath
        filePath = S_fileName           // S_fileName is set by Open/D
        if (strlen(filePath) == 0)      // User cancelled?
            return -2
        endif
    endif

    // Generate a wave name based on the file name
    String name = ParseFilePath(3, filePath, ":", 0, 0)     // Get file name without extension
    name = CleanupName(name, 0)                     // Make name kosher.
   
    // Generate parameter for LoadWave /B flag to set the wave name
    String columnInfoStr = "N=" + name + ";"

    // Load the data as 1D
    LoadWave/G/D/P=$pathName/O/A/B=columnInfoStr filePath
    if (V_flag == 0)                    // No waves loaded?
        Beep
        Print "No data found in file"
        return -3                   // Something went wrong
    endif
   
    // Create a wave reference for the wave created by LoadWave
    Wave w = $name
   
    // Redimension as 2D assuming square image
    Variable numPoints = numpnts(w)
    Variable numRows = sqrt(numPoints)
    Variable numColumns = numRows
    if (numRows*numColumns != numPoints)
        Beep
        Printf "NOTE: LoadSquareImageFrom1DDataFile expected a square array but got %d points\r", numPoints
    endif
    Redimension /N=(numRows,numColumns) w
   
    // Create an image plot
    String plotName = name + "_G"       // G for "graph"
    if (strlen(plotName) > 29)          // Name are limited to 31 character and we have to
        plotName = "Image"              // leave room for possible digits to make name unique
    endif
    if (Exists(plotName)!=0 || WinType(plotName)!=0)            // Name already in use?
        plotName = UniqueName(plotName, 6, 0)
    endif
    NewImage /N=$plotName w
   
    // Set plot title
    DoWindow/T $plotName, name
   
    return 0
End

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More