Column Names from Header in delimited text file
millot1
I d like to read a .txt file containing a header where lines start by a "# ", followed by 5 space delimited column of numbers.
The last line of the header contains the names of the columns starting with a "# " and space delimited.
Looking at earlier posts on the forum I got
Function ReadHeaderAndData(pathName, fileName, extension)
String pathName // Name of symbolic path or "" to display dialog.
String fileName // Name of file or "" to display dialog. Can also be full or partial path relative to symbolic path.
String extension // e.g., ".dat" for .dat files. "????" for all files.
setdatafolder root:
Variable refNum
// Possibly display Open File dialog.
if ((strlen(pathName)==0) || (strlen(fileName)==0))
Open /D /R /P=$pathName /T=(extension) refNum as fileName
fileName = S_fileName // S_fileName is set by Open/D
if (strlen(fileName) == 0) // User cancelled?
return -1
endif
// fileName is now a full path to the file.
endif
// Read the header lines
Open /P=$pathName /R refNum as fileName
// Read header here using FReadLine
string buffer
string/g header
string lastline
variable numline=0,Cond=1
do
FReadLine refNum, buffer
if(strsearch(buffer,"#",0)==0)
header+=buffer
Numline+=1
LastLine=buffer
else
Cond=0
endif
while (Cond==1)
Close refNum
// print header
Variable linesToSkip = Numline
Variable linesToLoad = 0 // Number of lines to load or 0 for auto (load all lines).
Variable columnsToSkip = 0 // Number of columns to skip. Skips date/time data and unit number.
Variable columnsToLoad = 0 // Number of columns to load or 0 for auto (load all columns).
Variable makeTable = 1 // 1 == make a table showing new waves.
LoadWave/Q/O/J/D/P=$pathName/K=0/L={0,linesToSkip,linesToLoad,columnsToSkip,columnsToLoad} /V={" ", "", 0, 1} fileName
return 0
End
String pathName // Name of symbolic path or "" to display dialog.
String fileName // Name of file or "" to display dialog. Can also be full or partial path relative to symbolic path.
String extension // e.g., ".dat" for .dat files. "????" for all files.
setdatafolder root:
Variable refNum
// Possibly display Open File dialog.
if ((strlen(pathName)==0) || (strlen(fileName)==0))
Open /D /R /P=$pathName /T=(extension) refNum as fileName
fileName = S_fileName // S_fileName is set by Open/D
if (strlen(fileName) == 0) // User cancelled?
return -1
endif
// fileName is now a full path to the file.
endif
// Read the header lines
Open /P=$pathName /R refNum as fileName
// Read header here using FReadLine
string buffer
string/g header
string lastline
variable numline=0,Cond=1
do
FReadLine refNum, buffer
if(strsearch(buffer,"#",0)==0)
header+=buffer
Numline+=1
LastLine=buffer
else
Cond=0
endif
while (Cond==1)
Close refNum
// print header
Variable linesToSkip = Numline
Variable linesToLoad = 0 // Number of lines to load or 0 for auto (load all lines).
Variable columnsToSkip = 0 // Number of columns to skip. Skips date/time data and unit number.
Variable columnsToLoad = 0 // Number of columns to load or 0 for auto (load all columns).
Variable makeTable = 1 // 1 == make a table showing new waves.
LoadWave/Q/O/J/D/P=$pathName/K=0/L={0,linesToSkip,linesToLoad,columnsToSkip,columnsToLoad} /V={" ", "", 0, 1} fileName
return 0
End
Is there an easy way to pass the column names (that we could get from
LastLine
using sscanf
) to LoadWave
August 10, 2015 at 08:19 pm - Permalink