Fix Line Breaks And Load - Illustrates Use of Temporary File in Data Loading

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

//  This is an illustration of loading data from a weird file format.
//
//  In some cases, the simplest approach is to create a cleaned-up version of the file
//  as a temporary file and then to load the data from that.
//
//  In this example, the file has line breaks to limit each line to 80 characters.
//  This breaks up a single line of data into multiple lines.
//
//  We create a temporary file with the bad line breaks removed and load the temporary file.
//  You will need to tweak this for your own situation.
//
//  Please read the code and comments below before using or tweaking for your data file.

Menu "Load Waves"
    "Fix Line Breaks and Load...", FixLineBreaksAndLoad("", "")
End

//  FixText(textIn)
//  Returns cleaned up version of text without bad line breaks.
//  To keep it simple, this routine assumes that each original line of data
//  has been split into exactly two lines.
//  We also assume that the terminator is CRLF.
//  We also assume that there is a terminator for the last line of the file.
// NOTE: You will need to tweak this for your situation
static Function/S FixText(textIn)
    String textIn
   
    String textOut = ""
   
    String terminator = "\r\n"
    Variable terminatorLen = strlen(terminator)
   
    Variable offset = 0
    do
        Variable pos
       
        // Remove the bad terminator
        pos = strsearch(textIn, terminator, offset)
        if (pos < 0)
            if (offset == 0)
                DoAlert 0, "The specified terminator was not found in the file"
            endif      
            break               // No more terminators
        endif
        textOut += textIn[offset,pos-1]
        offset = pos + terminatorLen
       
        // Skip the good terminator
        pos = strsearch(textIn, terminator, offset)
        if (pos < 0)
            break               // No more terminators
        endif
        textOut += textIn[offset,pos-1] + terminator
        offset = pos + terminatorLen
    while(1)
   
    return textOut
End

//  FixLineBreaksAndLoad(pathName, fileName)
//  A data file has unwanted line breaks for lines longer than 80 characters
//  This routine creates a temporary version of the file without the bad line breaks
//  and loads data from the temporary file.
Function FixLineBreaksAndLoad(pathName, fileName)
    String pathName     // Name of an Igor symbolic path or "".
    String fileName         // Name of file or full path to file.

    Variable refNum

    // First get a valid reference to a file.
    if ((strlen(pathName)==0) || (strlen(fileName)==0))
        // Display dialog looking for file.
        // Replace /T="????" with, for example, /T=".dat" if your files are .dat files.
        Open /D /R /P=$pathName /T="????" refNum as fileName
        fileName = S_fileName           // S_fileName is set by Open/D
        if (strlen(fileName) == 0)      // User cancelled?
            return -1
        endif
    endif
   
    // Open source file and read the raw text from it into a string variable
    Open/Z=1/R/P=$pathName refNum as fileName
    if (V_flag != 0)
        return -1                       // Error of some kind
    endif
    FStatus refNum                      // Sets V_logEOF
    Variable numBytesInFile = V_logEOF
    String text = PadString("", numBytesInFile, 0x20)
    FBinRead refNum, text               // Read entire file into variable.
    Close refNum
   
    // Fix the text
    text = FixText(text)                    // Remove bad line breaks
   
    // Write the fixed text to a temporary file
    String tempFileName = fileName + ".noindex" // Use of .noindex prevents Spotlight from indexing the file. Otherwise we get an error when we try to delete the file because Spotlight has it open.
    Open refNum as tempFileName
    FBinWrite refNum, text
    Close refNum   
   
    // Load the temporary file
    // NOTE: You will need to tweak this for your situation
    LoadWave/J/D/E=1/Q/P=$pathName tempFileName
    if (V_flag == 0)
        Printf "An error occurred while loading data from \"%s\"\r", S_fileName
    else
        Printf "Loaded data from \"%s\"\r", S_fileName
    endif

    // Delete the temporary file   
    DeleteFile /P=$pathName tempFileName
   
    return 0
End

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More