Function to Flip Trace Order in Graph

Hi all,

I wrote a small function to flip the order of traces in a graph:

Function FlipTraceOrder()
	String Traces = TraceNameList("",";",1)
	Variable NumTraces = ItemsInList(Traces)
	
	String NewList = ""
	Variable i
	For(i=0;i<NumTraces;i+=1)
		String CurrentTrace = StringFromList(NumTraces-i,Traces)
		NewList = AddListItem(CurrentTrace,NewList)
	EndFor
	
	ReorderTraces $StringFromList(0,Traces),{$NewList}
	
End

However, when I display a bunch of traces on a graph and then run this, it gives me the error, "error: trace is not on graph."

...except that it is. You know how I know for certain that it is? I pulled it directly from a list of the traces on this very graph using the "TraceNameList()" command at the beginning of the function.

Any ideas as to what I've done wrong?

Thanks!

-Will

Small update: changing the separator to "," instead of ";", as expected by ReorderTraces, does not seem to impact this problem.

Try this instead.

// @brief flips traces on graph
// @input gname = graph name as string (default frontmost)
Function FlipTraceOrder([string gname])

	string wname, cmdstr, nlist
	if (ParamIsDefault(gname))
		wname = ""
		gname = ""
	else
		wname = "/W=" + gname
	endif
	wave/T tlist = ListToTextWave(TraceNameList(gname,";",1),";")
	make/N=(DimSize(tlist,0))/FREE slist = p
	Sort/R slist, tlist
	wfprintf nlist, "%s,", tlist
	sprintf cmdstr, "ReorderTraces%s _front_, {%s}", wname, RemoveEnding(nlist)
	Execute cmdstr
  	return 0
  	  
end

The trick is to convert the list of traces into {...} not {"..."}. This change demands Execute.

To give a bit more context: Yes, you basically have to do it like JJ mentioned to reorder a list in one go. Note that in this case the number of items in the list is limited to 100 traces (the last time I checked). The issue with your code is that you try to feed in a string list via the $ prefix. This does not work, since it converts what ever is inside the string to a single reference (to a trace in this case). So a trace with the same name as you whole list content is obviously not in the graph. You could write a for loop to feed in single trace names this way to iteratively resort. However, the for-loop approach does not go well with trace instances, since the numbering gets updated each time the reordering is done.

Right- the way instance numbers work, the "name" of a trace can change if you move a trace forward or backward past another trace with the same name. We just recently fixed the command generation for the Reorder Traces and Reorder Images dialog to take changing instance numbers into account. That only took, like, 20 years or so 😬