Set the same name for waves

I'm new using IGOR and after reading several topics in the manual and posts in the forum I'm struggling with my procedure!
I've written a procedure to load all TXT files of a folder. As my text files have 2 columns IGOR automatically names them as wave0, wave1, wave2...
The first column of all files is the same. I have spectroscopy data, and the X axis is the same for all. So to be easy when plotting a graphic (I have 400 files that will be plotted in the same graphic then averaged) I'd like to rename all even waves (wave2, wave4, wave6..) as wave0 or any possible name. Is it possible?

Bellow is what I've written so far:
#pragma rtGlobals=3     // Use modern global access method and strict wave access.

function loadwaves(filename, pathname)
    string filename
    string pathname

    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif

    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
    break
    endif

    loadwave/a/g/o/d/p=$pathname filename
    if (v_flag==0)
    break
    endif
    index+=1
    while(1)
end


Thanks in advance :)
This is possible only if you place data from each file in it's own data folder. There can only be one Wave0 in a single data folder. Here, I want to suggest an alternative, since your X-axis is the same for all files, why not simply load the first one, and discard all the rest??? this way you have 1 Wave0 for your x-axis and then you'll have data waves for each file.

string s_wavenames //waves loaded by LoadWave
wave wavetokill = $(stringfromlist(0, s_wavenames, ";"))  //x-axis wave for current file

killwaves wavetokill      //kill the x-axis wave

rename $(stringfromlist(1, s_wavenames, ";")) cleanupname(removeending(s_filename, ".txt"))    //rename the data wave as the filename


I would also suggest either using the Rename operation to name your data waves something meaningful related to each file (perhaps the filename??) or using Note to add a note to each wave to describe it. Looking at the file later to see wave2, wave4, wave6.... will be confusing and difficult to discern what each wave represents.
Use the LoadWave /B flag to control the name of the wave as it is loaded.

See the section "Specifying Characteristics of Individual Columns" in the documentation for LoadWave.

There is an example of this at http://www.igorexchange.com/node/1538

For a more complex case you can programmatically generate the /B parameter string.

As proland points out, each wave in a given data folder must have a unique name.

Thank you proland!
Your suggestion and help about killing the x-wave was brilliant, it really worked and finally I can load the hundreds of spectra.

Although I'm still having some trouble with the rename function, more precisely with the RemoveEnding function. An error occurs and it says "To invoke a built-in function you must print or assign the result".
proland wrote:


string s_wavenames //waves loaded by LoadWave
wave wavetokill = $(stringfromlist(0, s_wavenames, ";"))  //x-axis wave for current file

killwaves wavetokill      //kill the x-axis wave

rename $(stringfromlist(1, s_wavenames, ";")) cleanupname(removeending(s_filename, ".txt"))    //rename the data wave as the filename



Above is your suggestion, so I've just added a Print command before the removeending function and I could compile:
rename $(stringfromlist(1, s_wavenames, ";")) cleanupname(Print removeending(s_filename, ".txt"))    //rename the data wave as the filename


But, when executing the rename another error appears "name already exists as a string function" . This one I couldn't solve, the waves were loaded but not renamed.

Thanks for the help!
Cheers
The following should work...

rename $(stringfromlist(1, s_wavenames, ";")), cleanupname(removeending(s_filename, ".txt"), 0)  

There needs to be a comma between the old name and new name in the rename operation. Also, cleanupname has a required parameter to determine whether liberal names can be used. The above line does not permit liberal wave names. This is the purpose of the final ", 0". You might look up rename, cleanupname and revomeending in command help for more details. Finally, if there is a chance that you might have duplicate files names (either from different directories or after cleaning them up), take a look at uniquename.
thanks, but this isn't working! An expected comma is missing after the 0. I would prefer to use 1, as the name will be liberal and my file names have space.

My procedure, with all alterations now looks like this:

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

function loadwaves(filename, pathname)
    string filename
    string pathname
    string s_wavenames    //waves loaded by Loadwaves

    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif

    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
    break
    endif

    loadwave/a/g/o/d/p=$pathname filename
    wave wavetokill = $(stringfromlist(0,s_wavenames, ";"))         //x-axis wave for current file
    killwaves wavetokill            // kill the x-axis
   
    rename $(stringfromlist(1, s_wavenames, ";")), cleanupname(Print removeending(s_filename, ".txt"), 1)  
    // rename the waves as filename
    //1 in cleanupname means the name is liberal, so dots andn spaces are accpeted
   
    if (v_flag==0)
    break
    endif
   
    index+=1
    while(1)
end


Thanks

Try this, the problem seems to be the placement of parentheses

rename $(stringfromlist(1, s_wavenames, ";"), cleanupname(removeending(s_filename, ".txt"), 1) )

EDIT: This solution is incorrect... see below.

By the way, why don't you load the x wave from one file outside the loop and then just load the second (data) wave from each file inside the loop? I don't know the syntax for this, but you should be able to specify specific column(s) to load w/o loading all columns. It really may not be all that more efficient in this case, but it would eliminate the needless killing of innocent waves. This comment comes from a reading of your initial post. In the code that you show just above, it doesn't appear that you are saving any x waves. Is this what you want?
jtigor, thanks for your idea! I've used the /B flag and loaded just the second column.
Unfortunately the rename isn't working. The same error of expecting a comma right after the 1 is still happening.

With all the changes the procedures now is:

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

function loadwaves(filename, pathname)
    string filename
    string pathname
    string s_wavenames    //waves loaded by Loadwaves

    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif

    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
    break
    endif
   
    loadwave/a/g/o/d/b="c=1, N='_skip_';" /p=$pathname filename
       
    rename $(stringfromlist(1, s_wavenames, ";")) cleanupname(print removeending(s_filename, ".txt"), 1) )
    // rename the waves as filename
    //1 in cleanupname means the name is liberal, so dots andn spaces are accpeted
   
    if (v_flag==0)
    break
    endif
   
    index+=1
    while(1)
end


I need to rename the waves as my file names once I have more than 400 files which correspond to spectra that need to be plotted in the same graph, for being averaged after!
If this procedure works it will save much time!
So thank you again :)
Enter the line as shown below. Your line of code has several errors.

There were errors in my advice offered several comments above. This one should be correct.

rename $(stringfromlist(1, s_wavenames, ";")), cleanupname(removeending(s_filename, ".txt"), 1)


I finally tested the rename line in Igor. This should work.

rename $stringfromlist(1, s_wavenames, ";"), $cleanupname(removeending(s_filename, ".txt"), 1)
The alterations really worked to compile the procedure, however when the function is used in the command line, error in the rename continuos to appear.

As I've skipped the first column of my data, in this rename parameter IGOR expects to have a name of wave, variable or string as the attached file shows.
If you still wanna take look go ahead!

Here the entire procedure:
#pragma rtGlobals=3     // Use modern global access method and strict wave access.

function loadwaves(filename, pathname)
    string filename
    string pathname
    string s_wavenames    //waves loaded by Loadwaves
    string s_filename

    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif

    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
    break
    endif
    loadwave/a/g/o/d/b="c=1, N='_skip_';"/p=$pathname filename
   
        // rename the waves as filename
    rename $stringfromlist(1, s_wavenames, ";"), $cleanupname(removeending(s_filename, ".txt"), 1)
   
        if (v_flag==0)
    break
    endif
   
    index+=1
    while(1)
   
end


Sorry for all these questionings.


I tried to compile your function with the problem line written like this

rename $(stringfromlist(1, s_wavenames, ";")), cleanupname(removeending(s_filename, ".txt"), 1)

and got the error as you describe.

But it compiles without error when written like this

rename $(stringfromlist(1, s_wavenames, ";")), $cleanupname(removeending(s_filename, ".txt"), 1)
jtigor wrote:
I tried to compile your function with the problem line written like this

rename $(stringfromlist(1, s_wavenames, ";")), cleanupname(removeending(s_filename, ".txt"), 1)

and got the error as you describe.

But it compiles without error when written like this

rename $(stringfromlist(1, s_wavenames, ";")), $cleanupname(removeending(s_filename, ".txt"), 1)




You mean the error about invoking a built-in function right?
With your help this was fixed, although a new one was created :P

In my previous message I attached a screenshot of the error when running the function in the command line..

cheers
This kind of statement is very difficult to debug:
rename $(stringfromlist(1, s_wavenames, ";")), $cleanupname(removeending(s_filename, ".txt"), 1)


I recommend writing it as:
String name = StringFromList(1, S_waveNames)
String newName = RemoveEnding(name, ".txt")
newName = CleanupName(newName, 1)
Rename $name, $newName


In this form it can be debugged with Print statements or using the Debugger.

The CleanupName call is not necessary. We know that name contains the name of a wave loaded by LoadWave. Therefore name must contain a valid name and it does not need to be cleaned up. It would need to be cleaned up if you wanted to force a standard rather than a liberal name. You would do this by changing the CleanupName parameter from 1 to 0.

Furthermore you can get rid of the rename altogether by properly using the /B flag, like this:

    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
        break
    endif
    String name = RemoveEnding(filename, ".txt")
    String columnInfo = ""
    columnInfo += "N='" + name + "';"
    columnInfo += "N='_skip_';"
    Print columnInfo        // For debugging only
    LoadWave/a/g/o/d/b=columnInfo /p=$pathname filename

I ran your function without getting the error that you mentioned (problem with rename function). This worked for me

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

function loadwaves(filename, pathname)
    string filename
    string pathname
    string s_wavenames    //waves loaded by Loadwaves
    string s_filename
 
    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif
 
    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")
    if (strlen (filename)== 0)
    break
    endif
    loadwave/a/g/o/d/b="c=1, N='_skip_';"/p=$pathname filename
 
        // rename the waves as filename
    rename $stringfromlist(0, s_wavenames, ";"), $cleanupname(removeending(s_filename, ".txt"), 1)
 
        if (v_flag==0)
    break
    endif
 
    index+=1
    while(1)
 
end


Are your sure that your files have the expected columns?

Howard's suggestions related to breaking up the rename command so that print statements can be used to diagnose the problem are very good. Is the debugger enabled and, if so, do you know how to use it?
Thank you so much jtigor and hrodstein!
Finally my procedure is working properly. I've used the /B flag as hrodstein suggested but replacing the first and second column info, once I needed to skip the first and rename the second.

Here is the final procedure, totally working if someone needs to load all files of a folder and skip/rename columns.

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

function loadwaves(filename, pathname)  //if you don't have a symbolic path use loadwaves("","") in command line to open a window to choose the folder
    string filename                
    string pathname
   

    if (strlen(pathname)==0)
    newpath/o temporaryPath
        if (v_flag!=0)
        return -1
        endif
    pathname= "temporarypath"
    endif

    variable index = 0
    do
    filename= indexedfile ($pathname, index, ".txt")    //All .txt files of the folder will be loaded. Change it if you have other file extension.
    if (strlen (filename)== 0)
        break
    endif
   
    String name = RemoveEnding(filename, ".txt")    //The name of my waves will be the same as the filename excluding the ".txt" from it.
    String columnInfo = ""                  //We are loading the waves using the /B flag, so here are the information about the columns.
    columnInfo += "N='_skip_';"             //IGOR will skip the first column, as they are the same for all my files. I'll load it manually.
    columnInfo +=  "N='" + name + "';"          //The second column (wave) will have its name replaced by the filename
    //Print columnInfo      // For debugging only

    LoadWave/a/g/o/d/b=columnInfo /p=$pathname filename //Loadwaves with its proper flags.
   
    if (v_flag==0)
    break
    endif
   
    index+=1
    while(1)
   
end



Thanks again to everyone who helped!!
Cheers