How to import a specific value from a plain text file in Igor Pro

I am working with a text file (attached) that contains various metadata and values written as plain text. I need to extract a few specific numeric values located at known line numbers and character positions from this file and import them into the Igor Pro data folder. From the attached text file, the specific values I need to extract are:

  1. Value '64.60' (TC of reflectance, lower split) occurring at line 71 and column 94
  2. Value '.61' (OC/TC) occurring at line 96 and column 19
  3. Value '.39' (EC/TC) occurring at line 97 and column 96

Each value occurs at a fixed position in the file.

I have already tried importing the text file using standard text import methods. However, since this file is not delimited, I am unable to access the value using column or row indices in the usual way. I previously attempted to use LoadWave with line and column limits, for example: LoadWave/J/D/E=1/K=0/V={" "," $",0,1}/L={82,85,1,0,0}/W file_path 

I would like to know if there is a way in Igor Pro to read a plain text file line by line and extract a value from a specific line and character position, or alternatively, to read the file as a string and extract the required value using string or substring operations. Any guidance, example code, or suggested functions for handling this kind of fixed-position text parsing would be greatly appreciated.

Sample Text File (9.85 KB)

For this you can use Open / Close and FReadLine, which reads one line per call. Below is a very crude but hopefully simple-to-understand example to read these values. This code has almost no safety checks and uses a brute force approach, but you should get the idea. The same basic approach can be used to write a sophisticated parser for data headers from text files, as I have done many times. For example one could rewrite the code to search for a specific entry label instead of specifying line and column numbers ...

Function LoadValFromFile(string fullPath, int line, int col)
 
    int ID, i
    Open/Z/R ID as fullPath
    if (V_flag)
        return NaN
    endif
    
    string read
    for (i = 0; i < line; i++)
        FReadLine ID, read
        if (!strlen(read))
            break
        endif
    endfor
    Close ID
 
    return str2num(read[col-1, inf])
End

Run via, e.g., (note that fullPath must be a mac-compatible path, since I did not bother to parse windows paths here):

print LoadValFromFile("C:Users:MyUserName:Desktop:textfile.txt", 96, 19)
  0.61

 

Here is a version similar as above, which uses a keyword to find the line of interest, then "sscanf" to extract numeric values.

function ReadLineByLine(S_fileName)
    String S_fileName       // full path to file to be loaded; "" for dialog
    variable refNum, v1, v2
        
    if(strlen(S_fileName) > 0)
        // full path is specified, load file directly
        Open/R refNum as S_fileName
    else
        // get dialog 
        Open/D/R/F="*.txt" refNum 
        if (strlen(S_FileName) == 0)
            return -1
        endif
        Open/R refNum as S_fileName
    endif
 
    do
        // read file line by line
        string line
        FReadLine refNum, line
        if(strlen(line) == 0)                       
            break                                       
        endif
        
        // EXAMPLE: look for keywords; then use sscanf to extract values
        if(StringMatch(line, "*OC/TC*"))
            print line
            sscanf line, "OC/TC: %f %f", v1, v2
            print v1, v2
        endif
    
    while(1)
    Close refNum
 
    return 1
end