How to solve this trace name issue?


I have hit this some time ago and solved this way described below. But I am bit puzzled since I would expect easier solution.

I have code which appends to graph waves - SourceInWv vs SourceQWv and adds error bars using wave SourceErrorWave

AppendToGraph /W=$(GraphWindowName) SourceIntWv vs  SourceQWv
ErrorBars /W=$(GraphWindowName)  $(NameOfWave(SourceIntWv)) Y,wave=(SourceErrorWv,SourceErrorWv)

Now, this is a loop going through number of wave triplets. If the names are unique, this is perfectly fine code. 

The trouble becomes when the wave names are the same and they are located in different folders. This is, unluckily, common case for me. In this case NameOfWave(SourceInWave)) returns name of wave which is not name name of trace -- but the trace name is needed for ErrorBars command. 

Now, my solution is to use /TN= to make sure I know what the trace name is:

AppendToGraph /W=$(GraphWindowName) SourceIntWv/TN=$(NewTraceName) vs  SourceQWv
ErrorBars /W=$(GraphWindowName)  $(NewTraceName) Y,wave=(SourceErrorWv,SourceErrorWv)

but now I have to worry about uniqueness of the NewTraceName, which previously I did not.

I searched through manual to figure out if there is something returned by AppendToGraph command to tell me, what I actually did append... I can surely interrogate recreation macros before and after to figure this out, but all of this seems ridiculously complicated for something which in its way is simple question: "what did I ended up appending"? 

I am sure there must be simple solution. Can someone educate me, please? Thank you... 


Great, so the last item on TraceNameList is what I added last. That is what I was missing here. 


But using TraceNameList does require calling DoUpdate before or? Otherwise it does not know about the new trace names IIRC.

@thomas_braun I don't think so. I haven't checked the manual but:

Function TestThis()
    Make/O/N=100 myData = gnoise(1)
    KillWindow/Z myPlot
    String myTrace
    Variable i
    for(i = 0; i < 5; i += 1)
        myTrace = "test_" + num2str(i)
        AppendToGraph/W=myPlot myData/TN=$myTrace
        Print TraceNameList("myPlot",";",1)




The need to call DoUpdate is driven by the fact that some members of the graph and trace data structures are only computed when the graph is drawn. So, for instance, the axis range after appending a trace will not be accurate unless you call DoUpdate.

But the trace name is set into the data structure when the name is given, so in this particular case DoUpdate is not needed. Our internal code may well need to look up the trace by name, so it has to be that way. Always good to verify, though.

To be sure, this is not a trivial point, as re-drawing the graph can be time-consuming.