Duplicate error: expected wave name

Hi everyone,

I have written a procedure in which I would like to automatically load some data from .csv files, concatenate them and sort the data using the first wave as a key. The problem is that the following function execution error appears: "While executing Duplicate, the following error occurred: expected wave name". I have recently started programming in Igor and do not know where the mistake is.

Thanks in advance, I would really appreciate any help you could provide!

Please find attached two .csv files as an example (Jan2015.zip).

Here is the procedure:
#pragma rtGlobals=1     // Use modern global access method.

//-----------------  Menu Code  -----------------
Menu "Intensidad Trafico Loader"
    "Intensidad Trafico Loader",  TraficoLoader()
End


Function TraficoLoader()
// This function should load files (with the edning ".csv") from the Intensidad Trafico Ayto. Madrid
    string fileList, tempfile
    variable iFile, numFile

    NewPath /M="Choose folder containing Intensidad Trafico Data files (ending '.csv')" RawDataPath
//  Index Directory
    fileList = IndexedFile(RawDataPath, -1, ".csv")
    print fileList
   
    numFile  = itemsinlist(fileList)

    if(datafolderexists("Intensidad_Trafico"))
        setdatafolder Intensidad_Trafico
    else
        newdatafolder /S Intensidad_Trafico
    endif

    For(iFile=0; iFile < numFile; iFile+=1)
        tempfile = stringfromlist(iFile, FileList)
       
        TraficoLoadDataFiles(tempfile)
    Endfor
End function

// -------------------------- -------------------------- -------------------------- --------------------------
   
Function TraficoLoadDataFiles(tempfile)
    string tempfile
    string ListOfWaves, WaveNm, NewNames
    variable numWaves, iWave
       
        // Names for the waves in the file
        NewNames = "idelem;fecha;identif;tipo_elem;tipo;intensidad;"
        NewNames += "ocupacion;carga;vmed;error;periodo_integracion;"
       
        if(!waveexists(idelem))
            LoadWave/J/D/A/W/B="C=1; F=8; F=-2; C=1; F=-2; C=1; C=1; C=1; C=1; F=-2; C=1;"/V={";","",1,0}/P=RawDataPath tempfile
           
            //rename the waves
            ListOfWaves = WaveList("wave*",";","")
            NumWaves = ItemsInList(NewNames)

            For(iWave=0; iWave<NumWaves; iWave+=1)
                wave wv1 = $stringfromlist(iWave, ListOfWaves)
                duplicate /O wv1 $stringfromlist(iWave, NewNames)
                killwaves /Z $stringfromlist(iWave, ListOfWaves)
            Endfor

            setdatafolder Intensidad_Trafico

        // If waves already exist, load into temp folder, then concatenate and delete folder
        else   
            newdatafolder /O/S temp
            LoadWave/J/D/A/W/B="C=1; F=8; F=-2; C=1; F=-2; C=1; C=1; C=1; C=1; F=-2; C=1;"/V={";","",1,0}/P=RawDataPath tempfile

            //rename the waves
            ListOfWaves = WaveList("wave*",";","")
            NumWaves = ItemsInList(NewNames)

            For(iWave=0; iWave<NumWaves; iWave+=1)
                wave wv1 = $stringfromlist(iWave, ListOfWaves)
                duplicate /O wv1 $stringfromlist(iWave, NewNames)
                killwaves /Z $stringfromlist(iWave, ListOfWaves)
            Endfor


            // Concatenate the waves
            numWaves = itemsinlist(NewNames)
            For(iWave=0; iWave < NumWaves; iWave +=1)
                WaveNm = stringfromlist(iWave, NewNames)
                wave TmpWv1 = $WaveNm
                wave TmpWv2 = ::$WaveNm
                concatenate {TmpWv1}, TmpWv2
            Endfor
           
            setdatafolder Intensidad_Trafico
            killdatafolder /Z temp
           
            // Sortwaves
            wave TempWv = $"idelem"
            Make /O/N=(numpnts(TempWv)) SortKey
            MakeIndex TempWv, SortKey
            for(iWave=0; iWave < numWaves; iWave +=1)
                WaveNm = stringfromlist(iWave, NewNames)
                wave TmpWv1 = $WaveNm
                IndexSort SortKey TmpWv1
            endfor         
            killwaves /Z  SortKey
            setdatafolder Intensidad_Trafico
        endif  
End function
Jan2015.zip (1.11 MB)
Hi Marta,

the flags you use with LoadWave already assign names to your waves and thus
ListOfWaves = WaveList("wave*",";","")
stores an empty string in ListOfWaves.

Right-click in your code and turn on "enable debugger" and "debug on error". It will jump to the code causing trouble during run-time.

As a specific solution I would suggest that you check if "ListOfWaves" is empty and only run the renaming code in this case.
As a coarse method you could use
If (StrLen(ListOfWaves))
or
If (ItemsinList(ListOfWaves)>0)
or even someting like
If (ItemsinList(ListOfWaves)==NumOfExpectedItems)
(plus EndIf's)

HJ
Thank you very much for your help, HJDrescher! I have fixed the problem!