change text following the wave symbol in the legend's annotation

In a folder, I have a set of waves. On each graph I plot 2 wave traces with a default legend. I want to change the text following the wave symbol in the legend's annotation while keeping the legend symbol in place. However, when I run the code below, the legend's symbol is replaced with a question mark

Function legend_change()
Variable index = 0
String  list = WaveList("*", ";", "")
Variable numItems=ItemsInList(list)
String theWave
for (index=0;index <numItems;index+=1)  
theWave = StringFromList(index, list)
Display /W=(610,132,1501,633) $theWave[*][1],$theWave[*][2]
Legend/C/N=text0/F=0/A=MC/X=40.25/Y=32.33
Legend/C/N=text0/J "\\s(theWave#1) new_name_for_wave1 \r\\s(theWave#2) new_name_for_wave2"
endfor
End


I get the same ?? when instead of
Legend/C/N=text0/J "\\s(theWave#1) new_name_for_wave1 \r\\s(theWave#2) new_name_for_wave2"


I used
Wave/Z w = WaveRefIndexed("", index, 4)


I figured it's something to do with string escape calling a wave from the legend but could not find a way to make it work.

Thanks!


Here is what you need, or at least close to it:
Function legend_change()
    Variable index = 0
    String  list = WaveList("*", ";", "")
    Variable numItems=ItemsInList(list)
    String theWave
    for (index=0;index <numItems;index+=1)  
        theWave = StringFromList(index, list)
        Display /W=(610,132,1501,633) $theWave[*][1],$theWave[*][2]
       
        // Start of changed lines
        String legendTextForTrace1 = "Hello"
        String legendTextForTrace2 = "Goodbye"
        String format = "\\s(%s#1) %s\r\\s(%s#2) %s"
        String text
        sprintf text, format, theWave, legendTextForTrace1, theWave, legendTextForTrace2
        Print text  // For debugging only
        Legend/C/N=legend0/F=0/A=MC/X=40.25/Y=32.33 text
        // End of changed lines
    endfor
End

hrodstein wrote:
Here is what you need, or at least close to it:
Function legend_change()
    Variable index = 0
    String  list = WaveList("*", ";", "")
    Variable numItems=ItemsInList(list)
    String theWave
    for (index=0;index <numItems;index+=1)  
        theWave = StringFromList(index, list)
        Display /W=(610,132,1501,633) $theWave[*][1],$theWave[*][2]
       
        // Start of changed lines
        String legendTextForTrace1 = "Hello"
        String legendTextForTrace2 = "Goodbye"
        String format = "\\s(%s#1) %s\r\\s(%s#2) %s"
        String text
        sprintf text, format, theWave, legendTextForTrace1, theWave, legendTextForTrace2
        Print text  // For debugging only
        Legend/C/N=legend0/F=0/A=MC/X=40.25/Y=32.33 text
        // End of changed lines
    endfor
End


Ooops, sorry! Yes, it does work. Thanks!