Reverse Trace Sorting with Trace Instances
I have an application which works on fake waterfall graphs, i.e., several traces separated by an increasing trace offset. Now, I want to provide a functionality to reverse the trace sorting (make the first trace the last, the second the second last and so on). I have written a small procedure involving ReorderTraces:
String Traces = TraceNameList("",";",1)
Variable Items = ItemsInList(Traces), i
for (i = 0; i < Items; i += 1)
ReorderTraces $StringFromList(0,Traces),{$StringFromList(Items - 1 - i,Traces) }
endfor
End
Above code works fine for traces of individual waves where the trace name is unique. But the code fails horribly when I display traces from columns of a 2D wave where the traces get instances with the same name (#1,#2, and so on). I guess the instances get renamed as soon as the first ReorderTraces command finishes, which garbles up the whole list. Maybe if I fetch the trace list after every call and try to sort out things from there it may work. But then there may be problems if only some traces are instanced. Anyway, I want to ask anyway if somebody has an idea for an elegant way to do this. I attach a test experiment to play around with.
Forum
Support
Gallery
Igor Pro 9
Learn More
Igor XOP Toolkit
Learn More
Igor NIDAQ Tools MX
Learn More
Looks like a bug to me. No matter what I do
•ReorderTraces test,{test#9,test#8,test#7,test#6,test#5,test#4,test#3,test#2,test#1}
the order in the ""Reorder traces" dialog is the same.
August 17, 2018 at 02:23 am - Permalink
I see. To add to my previous post, I am working on Igor Pro 8.00 64 Bit on Win 8.1
August 17, 2018 at 02:54 am - Permalink
You might consider renaming your traces as they are added to the plot:
In addition to working around a possible bug or quirk, it may help maintain your sanity.
August 17, 2018 at 05:29 am - Permalink
It's true- as soon as you reorder the traces, the instance numbers change. The instance number is a historical oddity that originally didn't cause a lot of problems- adding a wave to a graph twice was something that was only useful for special effects. But with the advent of data folders and subrange display, lots of perfectly reasonable and ordinary graphs wind up with multiple instances, yours being a prime example. Since changing the order of traces using instance numbers effectively renames the traces, it's not clear that ReorderTraces can realistically be made to work on that example.
Too bad we didn't start out with the equivalent of /TN=unique_trace_name.
Be aware that you can use /TN to give two traces the same user-specified name, in which case you get back to instance numbers. That would be a Bad Idea, but Igor doesn't let you know that you've done it.
Perhaps we need a new command: ModifyGraph rename(oldtracename)=newtracename
or something.
For your graph with columns from a matrix, you might want to do something like
AppendToGraph matrixwave[column]/TN=$(NameOfWave(matrixwave)+"_"+num2str(column))
That will give you unique trace names to use with ReorderTraces, and the number will tell you exactly what the trace represents.
August 17, 2018 at 09:13 am - Permalink
Try this:
String Traces = TraceNameList("",",",1) // in back-to-front order, comma sep
if( strlen(Traces) )
Traces= ReverseList(Traces, ",")
Traces= RemoveEnding(Traces, ",")
String cmd="ReorderTraces _front_,{"+traces+"}"
Execute cmd
endif
End
Function/S ReverseList(list, sep)
String list
String sep
Variable i, n = ItemsInList(list, sep)
String reversed=""
for (i = 0; i < n; i += 1)
String item=StringFromList(n - 1 - i,list, sep)
reversed += item + sep
endfor
return reversed
End
Here are extracts of the before and after recreation macros:
PauseUpdate; Silent 1 // building window...
Display /W=(59.25,52.25,447,356.75)/K=1 test[*][9],test[*][8],test[*][7],test[*][6] as "Waterfall of test"
AppendToGraph test[*][5],test[*][4],test[*][3],test[*][2],test[*][1],test[*][0]
ModifyGraph mode=7
...
EndMacro
Window WaterfallGraph0After() : Graph
PauseUpdate; Silent 1 // building window...
Display /W=(17.25,40.25,405,344.75)/K=1 test[*][0],test[*][1],test[*][2],test[*][3] as "Waterfall of test"
AppendToGraph test[*][4],test[*][5],test[*][6],test[*][7],test[*][8],test[*][9]
ModifyGraph mode=7
...
EndMacro
August 17, 2018 at 09:44 am - Permalink
Thanks Jim. I've learned something!
August 17, 2018 at 02:21 pm - Permalink
Wow. Impressive, Jim!
August 17, 2018 at 02:53 pm - Permalink
Wow, thanks a lot. This solution works great, and I learned yet an new feature. I actually use ReorderTraces _back_ in my code, because I sometimes have a trace attached to the front which should stay put, but the result is just the same. If one would want to make the code Igor 6 compatible, maybe briefly attaching a dummy wave as anchor would work here (I didn't test this).
Anyway thank you all a lot for your help.
August 19, 2018 at 09:05 pm - Permalink