Add a trace and update the average trace

For example, use code like this to make a graph with dummy data.

Make /o Test1=gnoise(1),Test2=gnoise(2),Test3=gnoise(3),Test4=gnoise(4),Test5=gnoise(5)
Display Test1,Test2
ControlBar /T 30
Variable /G traceNum=2
SetVariable traceNum,value=traceNum,title="#"
Button myButton, proc=AddTraceAndAverage, title="Add"


And then have a function like this in your procedure file.

Function AddTraceAndAverage(ctrlName)
  String ctrlName

  NVar traceNum
  AppendToGraph $("Test"+num2str(traceNum))  

  String traces=TraceNameList("",";",1)
  Variable i,count=0,displayAvg=1
  for(i=0;i<ItemsInList(traces);i+=1)
    String trace=StringFromList(i,traces)
    if(StringMatch(trace,"Avg")) // Average already displayed.  
      displayAvg=0 // Don't display it again.  
      continue
    endif
    Wave w=$trace
    if(i==0)
      Duplicate /o w,Avg // The average will have the same length and scaling as the first trace.  
    else
      Avg+=w
    endif
    count+=1
  endfor
  Avg /= count // Turn a sum into an average.  
  if(displayAvg) // Display the average?  
    AppendToGraph Avg
  endif
End

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More