duplicate waves in other folder

Hello, 

I had some problem with my following function, seems to not work however running, making the while loop, have anyone an idea?

thanks for help

Function duplicate_list(wave_name)
    String wave_name
   
    String list = wavelist(wave_name,";","")
    Variable index = 0
    String wName
   
   
    do
     setdatafolder root
        wName = StringFromList(index, list)
        if (strlen(wName) == 0)
            break
        endif
        Wave in_wave = $wName
       
        String newName
        newName = wName  
       
       
        setdatafolder refit
        duplicate   $wName, $newName
       
       
       
        index++;
       
    while(1)
   
End Function

 

Welcome to the forum!

It is not clear (to me at least) what you are trying to achieve and what is not working exactly. The code you posted does not seem to be that useful. For example, you use WaveList() but in a way which only ever creates a list with one member. It seems to me you are trying to duplicate a list of waves into another folder, but it is difficult to propose something without knowing what you are trying to do. Can you tell us a bit more?

In reply to by chozo

hello,

thanks for your fast answer! Yes, my purpose is to copy in folder named refit a list of waves called " wavename* " where * varies with index.

Initially the waves to copy are in root.

The function runs , enters in the loop 5 times till break ( 5 is the number of waves to copy for the test) but at the end no waves in the refit folder and a message "While executing SetDataFolder, the following error occurred: No child data folder of that name exists."

I precise that refit folder does exist in root, created before the function runs.

thanks for your help

 

In reply to by valhal

Function duplicate_list(wave_name)
    String wave_name
   
    String list = wavelist(wave_name,";","")
    Variable index = 0
    String wName
   
    NewDataFolder/O root:refit
    DFREF dfr = root:refit
   
    do
     //setdatafolder root
     
        wName = StringFromList(index, list)
        if (strlen(wName) == 0)
            break
        endif
        Wave in_wave = $wName
       
//        String newName
//        newName = wName  
       
       
//        setdatafolder refit
        duplicate   in_wave, dfr:$wName
       
       
       
        index++;
       
    while(1)
   
End // Function

I have edited your function, commenting out some lines that aren't needed and adding a couple of lines to create a new data folder.

In reply to by tony

if you are using Igor Pro 9, you can do it like this:

function duplicate_list(string wave_name)
    string list = WaveList(wave_name, ";", "")
    variable numWaves = ItemsInList(list)
    Make/free/N=(numWaves)/wave wWaves = $StringFromList(p, list)
   
    NewDataFolder/O root:refit
    DFREF dfr = root:refit
   
    for(wave w : wWaves)
        Duplicate w dfr:$NameOfWave(w)
    endfor
end

 

This will do a more generic duplication and has error checking. It could be improved also with Tony's Igor Pro 9 version code.

// duplicate from srcdf (string full path) to todf (string full path)
// empty strings input will use current data folder
Function duplicate_WavestoDF(string srcdf, string todf)

    variable ic, Nwaves
    string theWName, LoWtoDuplicate

    if (strlen(srcdf) == 0)
        srcdf = GetDataFolder(1)
    endif
    if (strlen(todf) == 0)
        todf = GetDataFolder(1)
    endif
   
    if (!DataFolderExists(srcdf) || !DataFolderExists(todf))
        print "No duplication ... source or to data folder does not exist"
        return -1
    endif
   
    DFREF SDF = $srcdf
    DFREF TDF = $todf
   
    LoWtoDuplicate = WaveList("*",";","",SDF)
    NWaves = ItemsInList(LoWtoDuplicate)
    if (NWaves == 0)
        return -1
    endif
   
    DFREF cdf = GetDataFolderDFR()
    SetDataFolder TDF
    for (ic=0;ic<NWaves;ic+=1)
        theWName = StringFromList(ic,LoWtoDuplicate)
        wave/SDFR=SDF wtd = $theWName
        duplicate/O wtd $theWName
    endfor
    SetDataFolder cdf
    print "Duplication of ", NWaves, " waves from ", srcdf, " to ", todf
    return 0
end