Waves, references to waves, and names of waves

In the Igor Guided Tour 3 there is the following function:

Function AppendResiduals(yWave, xWave)
    Wave yWave
    Wave/Z xWave        // X wave or null if there is no X wave

    if (WaveExists(xWave))
        AppendToGraph/L=Lresid yWave vs xWave
    else
        AppendToGraph/L=Lresid yWave
    endif
    String traceName = NameOfWave(yWave)
    SetAxis/A/E=2 Lresid
    ModifyGraph nticks(Lresid)=2,standoff(bottom)=0,axisEnab(left)={0,0.7}
    ModifyGraph axisEnab(Lresid)={0.8,1},freePos(Lresid)={0,kwFraction}
    ModifyGraph mode($traceName)=2,lsize($traceName)=2
End

Why do we make a string called traceName, and then when we use it we use the $ operator to turn it back into a reference? Why not just do

ModifyGraph mode(yWave)=2

I've seen this format a couple of times, where you make a string with the name of the wave, and then do $string. Why do this? Why not just pass the name of the wave object, e.g. ywave?

Because the name of the actual wave isn't (likely to be) "yWave".

The function's yWave parameter is a name local to the function used as a wave reference to the wave passed to the function.

NameOfWave gets the actual name (not the local reference name) of that wave. The wave's actual name is needed in ModifyGraph; the local one won't do.

Part of your confusion may be the fact that a trace name is not the same as the trace's wave name. At least, not necessarily! The trace name might include something like "#1" if you have more than one trace for the same wave, or a second trace showing a wave with the same name (but in a different data folder). Or you might have used the /TN flag to set your own trace name.

In fact, that code makes an assumption that the trace name *is* the same as the wave's name. That is *almost* always true. For that function, that is more likely to be true than for other uses, since a new wave will be made holding the residuals. Still, it's something that programmers doing non-trivial programming need to be aware of.

The $ operation converts a string to a name.

Function Demo()
    String s = "wave0"      // A string containing a name
   
    // Make requires a name, not a string
    Make/O $s
   
    // A wave declaration requires a name on the righthand side, not a string
    WAVE w = $s         // w is a wave reference
   
    // Display requires a wave reference
    Display w
   
    // ModifyGraph requires a trace name, not a string or a wave reference 
    ModifyGraph mode($s) = 0
   
    AppendToGraph w         // Add another trace showing wave0
   
    // #1 is "instance notation" to distinguish multiple traces from same wave
    String t = "wave0#1"
   
    // ModifyGraph requires a trace name, not a string or a wave reference
    ModifyGraph mode($t) = 3
End

 

 

 

Thanks all for the helpful responses. I also shared them with my coworkers, who were confused about this issue too.