Trying to load files from a folder
rvasdev
I am new to IGOR and trying to create a procedure to lad all csv files from a folder. The files all have two columns and I want them to be plotted. I have attached the code I have but the problem I'm facing is that the code only uploads two waves wave0 and wave1 and not all the files from the folder.
#pragma rtGlobals=3 // Use modern global access method and strict wave access.
// Get the folder path.
Menu Reactor;
Load Reactor, LoadDataFromCSV()
End
Function LoadDataFromCSV()
string folderPath
string fname
string wname
string sname
variable i
getfilefolderinfo/D
newpath/O GCFolderData S_path
string fileList= indexedfile(GCFolderData,-1,".csv")
do
//store the ith name in the list into wname.
fname = stringfromlist(i,fileList)
//!!!!!!!!!! change the next line if you want a different name for the waves that are created !!!!!!!!!!!!!!!
wname = fname
sname = stringfromlist(i,filelist)
wave wave0 = $wname
wave wave1 = $sname
//if the referenced wave does not exist, create it.
if (!waveexists(wave0)&!waveexists(wave1))
//The /L parameter tells IGOR to load no headers, and to load the 3rd column of data (indexed as 2) only
//!!!!!!!! You must change this next line to tell IGOR how to load the data in each file !!!!!!!!!!!!!!!
LoadWave/J/N=wave/P=GCFolderData/O/L={0,0,0,0,0} stringfromlist(i,filelist)
LoadWave/J/N=wave/P=GCFolderData/O/L={0,0,0,1,0} stringfromlist(i,filelist)
print "Loaded "+fname
rename wave0 $wname
rename wave1 $sname
else
print fname+" was previously loaded. Its corresponding wave exists."
endif
i += 1 //move to next file
while(i<itemsinlist(filelist)) //end when all files are processed.
end
End
// Get the folder path.
Menu Reactor;
Load Reactor, LoadDataFromCSV()
End
Function LoadDataFromCSV()
string folderPath
string fname
string wname
string sname
variable i
getfilefolderinfo/D
newpath/O GCFolderData S_path
string fileList= indexedfile(GCFolderData,-1,".csv")
do
//store the ith name in the list into wname.
fname = stringfromlist(i,fileList)
//!!!!!!!!!! change the next line if you want a different name for the waves that are created !!!!!!!!!!!!!!!
wname = fname
sname = stringfromlist(i,filelist)
wave wave0 = $wname
wave wave1 = $sname
//if the referenced wave does not exist, create it.
if (!waveexists(wave0)&!waveexists(wave1))
//The /L parameter tells IGOR to load no headers, and to load the 3rd column of data (indexed as 2) only
//!!!!!!!! You must change this next line to tell IGOR how to load the data in each file !!!!!!!!!!!!!!!
LoadWave/J/N=wave/P=GCFolderData/O/L={0,0,0,0,0} stringfromlist(i,filelist)
LoadWave/J/N=wave/P=GCFolderData/O/L={0,0,0,1,0} stringfromlist(i,filelist)
print "Loaded "+fname
rename wave0 $wname
rename wave1 $sname
else
print fname+" was previously loaded. Its corresponding wave exists."
endif
i += 1 //move to next file
while(i<itemsinlist(filelist)) //end when all files are processed.
end
End
Execute this Igor command to see a help topic that might help with the "all the files" part of the problem: DisplayHelpTopic "Loading All of the Files in a Folder"
Then add to it the graphing part of the problem. You may wish to examine other examples within that help file.
February 7, 2024 at 10:19 am - Permalink
Load Reactor, LoadDataFromCSV()
End
Here you need quotes around the strings
// - snip -
wname = fname
sname = stringfromlist(i,filelist)
in these lines you set fname, wname and sname to the same value.
if (!waveexists(wave0)&!waveexists(wave1))
& is a bitwise operator. In this instance it will work, but better to use && where you mean logical AND.
wave wave0 = $wname
if there is a chance the wave does not exist, use wave/Z
LoadWave/J/N=wave/P=GCFolderData/O/L={0,0,0,1,0} stringfromlist(i,filelist)
print "Loaded "+fname
rename wave0 $wname
rename wave1 $sname
Here you use the /N=basename and the /O (overwrite) flag. Consider instead using the /A flag and extracting the wavename from S_waveNames in order to build your rename operation.
I suggest:
// check for success here
rename $StringFromList(0, s_wavenames) $wname
rename $StringFromList(1, s_wavenames) $sname
There may be other errors, these are what caught my eye.
Don't be afraid to experiment with the debugger early in your programming adventures!
execute DisplayHelpTopic "The Debugger"
February 7, 2024 at 11:06 am - Permalink