How to use Killwaves and WaveExists operation for a bunch of waves?

Hello all,

I have two questions.

1) I was wondering if there is a way to use a bunch of indexed, and without indexed waves as the argument of 'killwaves' and 'WaveExists' operation. It seems like 'WaveExists' operation just take only one wave as its argument and in the case of 'killwaves' operation, one needs to manually put all the names of the waves as its arguments which is tedious. Let me explain a little bit more.

I have waves named a_00, a_01,....., a_60, b_00, b_01,....,b_60 (indexed waves), and df, vc, iu, kl, qu,...... (unindexed waves like this). I want to use both the 'killwaves' and 'WaveExists' operation on these waves. I tried to use a loop putting the names of the waves in a string separated by ';', but each time Igor complains about the length of the string (I clearly understand the string is too long to handle by Igor in this case).

 

2) Is there a way to update a named table without creating any extra indexed table at the same table name? If I run the program, every time it creates a new indexed table at the same name which I don't want.  

 

Thanks a lot to everybody,

Ann

 

 

Hi Ann

Where is the error about "string too long" raised? Strings in Igor can be arbitrarily long, though wave names not. There is also no real need to test whether a wave exists or not of you use KillWaves/Z like in the example below:

Function KillWavesInList(WvList)
    String WvList
   
    Variable i,j = ItemsInList(WvList)
    String WvName
   
    for (i=0;i<j;i+=1)
        WvName = StringFromList(i,WvList)
        KillWaves/Z $WvName
    endfor
End

 Or if you have Igor 9 you can be more efficient:

Function KillWavesInList(String WvList)
   
    for (String WvName : ListToTextWave(WvList,";"))
        KillWaves/Z $WvName
    endfor
End

 

Here is an example of how you can make KillWaves and WavesExists work with a wave reference wave

Function MyFunction()
    //  Creates a wave reference wave with 100 valid and 100 invalid wave references
    Make/O/WAVE/N=200 root:WaveRefWave/WAVE=WaveRefWave
    Variable i=0
    for (i=0; i<100; i+=1)
        Make/O/N=10 root:$("Wave"+Num2Str(i))/WAVE=MyWave
        WaveRefWave[i]=MyWave
    endfor
   
    //  Kills all waves in the wave reference wave
    Make/O/N=200 root:KillCounter/WAVE=KillCounter
    KillCounter[]=MyKillWaves(WaveRefWave[p])  
end



Function MyKillWaves(MyWave)
//  Kills MyWave and returns the WaveExists value from before the wave was killed
Wave/Z MyWave
Variable ReturnValue=WaveExists(MyWave)
    KillWaves/Z MyWave
    Return ReturnValue
end

 

You could also use Execute, but I think it's a little old fashioned and there is probably a limit to the length of the string

    String MyString="root:Wave10, root:Wave11, root:Wave12, root:Wave13"
    Execute/Q "KillWaves/Z "+MyString

 

If you create a table with Edit that table is updated every time any value in the displayed waves changes. You also cannot Kill a wave displayed in a table. You have to kill the table first, then you can kill the wave. If you want to add waves to an already existing table look at AppendToTable. I don't use tables much.

Hi all, 

Thank you very much for your suggestions. Now it is clear to me, killwaves can be used in a loop to kill all the indexed files easily. Just wondering, can 'WaveExists' be used to take multiple unindexed waves as its arguments?

Ann

 

Hi Ann

I'm a bit unclear why there should be a distinction between indexed and unindexed waves. Here is an example snippet that demonstrates that:

// makes some waves to use
Function MakeWaves()
   
    DFREF dfrSave = GetDataFolderDFR()
   
    NewDataFolder /O/S WaveExistsTest

    // make some waves in the current data folder
    Make /O a_00,a_01,a_60, b_00, b_01,b_60 // indexed waves
    Make /O df,vc,iu,kl,qu // unindexed waves
    String fakeWaves = "dog;cat;moose;cow_0;sheep_1;horse_2;" // 'waves' that dont exist
   
    String WvList = WaveList("*",";","") + fakeWaves
   
    DoTheseWavesExist(WvList)

    SetDataFolder dfrSave // return to where we started
End

// loop through a list of possible wave names and print in the history area if they exist
Function DoTheseWavesExist(WvList)
    String WvList
   
    Variable i,j = ItemsInList(WvList)
    String WvName
   
    for (i=0;i<j;i+=1)
        WvName = StringFromList(i,WvList)
        printf "'%s' %s exist!\r",WvName, SelectString(WaveExists($WvName),"does not","does")
    endfor
End

If you call MakeWaves() then in the history area the wave name and whether it exists or not will be printed in a list. You can check the new data folder that is created called 'WaveExistsTest' to confirm if it is true or not.

WaveExists takes a single wave reference as input and returns the truth (0 or 1) as to whether the reference points to an existing wave. It's hard to see what it would do with multiple inputs. Perhaps you want something like what this sequence of commands illustrates:

make/WAVE/N=3 threewaves  // make a wave reference wave with three elements
make/n=3 threewavesexist  // make a results wave for wave exists
make ddd   // make some random wave
•threewaves[0] = ddd  // add a reference to it in the wave reference wave
•threewavesexist = waveexists(threewaves[p])  // fill the results wave with truth as to whether the references in the wave reference wave are valid
print threewavesexist
  threewavesexist[0]= {1,0,0}

So one wave of interest was made, and a reference to it was put into the zero row of the wave reference wave. The other elements were never assigned; applying WaveExists to those references will return zero. The final print shows that is exactly the case- row zero of the results wave is 1 indicating that the row 0 wave reference points to a valid wave.

Thanks a lot everybody for your suggestions and tips. Now I got it.

 

Ann