Generating New Graph from Top Graph Waves in script

I have created a script to take the waves from my top graph and generate new waves that have been differentiated and smoothed. This part of the script works great, but to save time during my data workup, I have been trying to get the script to generate a new graph with just the new waves. But since my script uses a 'For' loop, every attempt to have it generate a new graph causes the script to recognize it as the new top graph, which doesn't contain data. Thus the script ends because there is no more data that it recognizes. This may just be an issue with order of operations in the commands, but I can't seem to figure out how to fix it. Any thoughts?

Function bulk_difsmth()
    string list = tracenamelist("",";", 1)   //get list of normal traces on topmost graph

    Display

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
    wave ywave = TraceNametoWaveRef("", stringfromlist(i, list, ";"))   //trace i from top most graph
    wave xwave = XwaveRefFromTrace("", stringfromlist(i, list, ";"))     //x wave associated with trace i

        String yname = NameofWave(ywave)   //get the name of the y wave
        String dest_wave = yname + "_DIF"      //  add  _DIF to name to distinguish from source
        String dest_wave2 = dest_wave + "_smth"      //  add  _smth to name to distinguish from source

        Differentiate ywave/X=xwave/D=$Dest_Wave;DelayUpdate    //use string Dest_Wave to name the destination wave based on the y-wave name.
       
        Duplicate/O $Dest_Wave, $Dest_Wave2
        Smooth/EVEN/B 20, $Dest_Wave2;DelayUpdate
       
        AppendtoGraph $Dest_Wave2 vs xwave
   
    endfor
End

 

When you use the Display command, specify a name for the window and then in the line AppendToGraph, specify the graph using the /W flag. If you will run it repeatedly, it's best to kill the window before creating it and/or check that the graph window name is unique.

In reply to by sjr51

I still run into the same issue as the Display command makes the new graph as the topmost graph. If I generate a graph first with the designated name and then run the script, then it works. Is there a way to create a new graph without it becoming the topmost graph?

Function bulk_difsmth()
    string list = tracenamelist("",";", 1)   //get list of normal traces on topmost graph

    Display /N=BulkDifSmthGraph

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
    wave ywave = TraceNametoWaveRef("", stringfromlist(i, list, ";"))   //trace i from top most graph
    wave xwave = XwaveRefFromTrace("", stringfromlist(i, list, ";"))     //x wave associated with trace i

        String yname = NameofWave(ywave)   //get the name of the y wave
        String dest_wave = yname + "_DIF"      //  add  _DIF to name to distinguish from source
        String dest_wave2 = dest_wave + "_smth"      //  add  _smth to name to distinguish from source

        Differentiate ywave/X=xwave/D=$Dest_Wave;DelayUpdate    //use string Dest_Wave to name the destination wave based on the y-wave name.
       
        Duplicate/O $Dest_Wave, $Dest_Wave2
        Smooth/EVEN/B 20, $Dest_Wave2;DelayUpdate
   
    AppendtoGraph /W=BulkDifSmthGraph $Dest_Wave2 vs xwave
   
    endfor

End

 

It's always a good idea to use explicit window names. Like this:

Function bulk_difsmth()

    String topGraph = WinName(0, 1)
    string list = tracenamelist(topGraph,";", 1)   //get list of normal traces on topmost graph

    Display /N=BulkDifSmthGraph

    variable i
    for (i=0; i<itemsinlist(list); i+=1)
    wave ywave = TraceNametoWaveRef(topGraph, stringfromlist(i, list, ";"))   //trace i from top most graph
    wave xwave = XwaveRefFromTrace(topGraph, stringfromlist(i, list, ";"))     //x wave associated with trace i

        String yname = NameofWave(ywave)   //get the name of the y wave
        String dest_wave = yname + "_DIF"      //  add  _DIF to name to distinguish from source
        String dest_wave2 = dest_wave + "_smth"      //  add  _smth to name to distinguish from source

        Differentiate ywave/X=xwave/D=$Dest_Wave;DelayUpdate    //use string Dest_Wave to name the destination wave based on the y-wave name.
       
        Duplicate/O $Dest_Wave, $Dest_Wave2
        Smooth/EVEN/B 20, $Dest_Wave2;DelayUpdate
   
    AppendtoGraph /W=BulkDifSmthGraph $Dest_Wave2 vs xwave
   
    endfor

End