Average all traces between marquee limits

I have written a code to average all traces on a graph between the marquee left and right limits. This code also tags each trace to display the average value. It is supposed to place the tag in the middle of the marquee limits, but it does not always work. For instance, if I have traces plotted versus different X waves (but on the same axis) and these X waves have a different number of datapoints in them, then some of those tags will be messed up (i.e., not placed in the right location, even if the displayed average number is correct).

You can see from the comments that I have tried a number of things. The version left in the actual code is the version that works for some traces, but not the for the waves with more indices (other versions I've tried mess up the placement of all tags). Any ideas?

I think the problem is that I can't figure out how to tell the Tag command what XWave to use when placing the tag. The only thing I can guess after reading through help files is that I need to declare an XY pair or do something with X scaling of the Y wave (those might be the same thing, I'm just unfamiliar with that part of Igor), but my X waves are not evenly spaced so I don't know if I can set a scale or not.

Thanks for the help.





Menu "GraphMarquee"
    "-"
    "Average the data", /Q, AveragetheData()
End


Function AveragetheData()
    GetMarquee left,bottom
   
    variable leftindex,rightindex,avgIndex,ii=0
    string TraceList,TraceName,TraceYName,TraceXName,tagName="tag" //,tagPosName="tagPos"
    TraceList=TraceNameList("",";",5) // bit 5 mean include normal and contour traces and omit hidden traces
//  TraceList=SortList(TraceList,";",1)
   
    do // loop over however many traces are on the plot
        TraceName=StringFromList(ii,TraceList)
        if(strlen(TraceName)==0)
            break
        endif
        //do something with tracename
//      print TraceName
//      print StringByKey("XWAVE",TraceInfo("",TraceName,0))
//      print StringByKey("XWAVEDF",TraceInfo("",TraceName,0))
       
        // Need to fix the fact that some traces have #1 or #2 etc behind them and this makes using them as wave names fail
//      sscanf TraceName, "%s%*[#]%f",TraceYName,dummyNum
//      print TraceYName
       
//      SplitString/E="*#" TraceName
//      Print S_value
       
        // this is not really a good solution, but should work for now
//      print ReplaceString("#1",TraceName,"")
        TraceYName=ReplaceString("#1",TraceName,"")
        TraceYName=ReplaceString("#2",TraceYName,"") // hopefully no more than this
       
        TraceXName = StringByKey("XWAVE",TraceInfo("",TraceName,0))
        SetDataFolder StringByKey("XWAVEDF",TraceInfo("",TraceName,0))
       
        FindLevel/Q $TraceXName,V_left
        leftindex=V_LevelX
        FindLevel/Q $TraceXName,V_right
        rightindex=V_LevelX
        avgIndex=(leftIndex+rightIndex)/2
//      avgIndex=(V_left+V_right)/2
//      print avgIndex
       
        WaveStats/Q/R=[leftindex,rightindex] $TraceYName
        // save results in variables/waves ?
       
        tagName+=num2str(ii)
//      tagPosName+=num2str(ii)
//      wave traceX=$TraceXName
//      tagPosition=traceX[avgIndex]
//      variable $tagPosName
//      variable tagPosition=$tagPosName
//      tagPosition=avgIndex
//      variable tagPosition
//      tagPosition=traceX[avgIndex]
        Tag/C/N=$tagName/F=0/L=1/TL=0 $TraceName, avgIndex,TraceName+" = "+num2str(V_avg)
        // even though avgIndex is correct, Tag is assigning things to the Dp wave instead of the longer fitX waves
       
        ii+=1
    while(1) // exit via break statement
End // AveragetheData
I've figured out the wave scaling with the help of a friend. Below is in the solution, in case anyone might find this code useful.

Suggestions are welcomed! You'll see in the comments that I still am not doing everything completely elegantly.


Menu "GraphMarquee"
    "-"
    "Average the data", /Q, AveragetheData()
End

Function AveragetheData()
    GetMarquee left,bottom
   
    variable leftindex,rightindex,avgIndex,delta,offset,tagPosition,ii=0
    string TraceList,TraceName,TraceYName,TraceXName,tagName="tag" //,tagPosName="tagPos"
    TraceList=TraceNameList("",";",5) // bit 5 means include normal and contour traces and omit hidden traces
   
    do // loop over however many traces are on the plot
        TraceName=StringFromList(ii,TraceList)
        if(strlen(TraceName)==0)
            break
        endif
       
        // Need to fix the fact that some traces have #1 or #2 etc behind them and this makes using them as wave names fail
//      sscanf TraceName, "%s%*[#]%f",TraceYName,dummyNum
//      print TraceYName
       
//      SplitString/E="*#" TraceName
//      Print S_value
       
        // this is not really a good solution, but should work for now
        TraceYName=ReplaceString("#1",TraceName,"")
        TraceYName=ReplaceString("#2",TraceYName,"") // hopefully no more than this
       
        TraceXName = StringByKey("XWAVE",TraceInfo("",TraceName,0))
        SetDataFolder StringByKey("XWAVEDF",TraceInfo("",TraceName,0))
       
        FindLevel/Q $TraceXName,V_left
        leftindex=V_LevelX
        FindLevel/Q $TraceXName,V_right
        rightindex=V_LevelX
       
        // for placement of the Tag
        avgIndex=(leftIndex+rightIndex)/2
        // account for possibility of scaled wave
        delta=dimdelta($TraceYName,0)
        offset=dimoffset($TraceYName,0)
        tagPosition=delta*avgIndex+offset
       
        WaveStats/Q/R=[leftindex,rightindex] $TraceYName
        // save results in variables/waves ?
       
        tagName+=num2str(ii)
        Tag/C/N=$tagName/F=0/L=1/TL=0 $TraceName, tagPosition,TraceName+" = "+num2str(V_avg)
       
        ii+=1
    while(1) // exit via break statement
End // AveragetheData