## Wave with Y values at a single X value from a graph

Average rating
(1 vote)

```Function extractGraphAtXValue(theX, theGraph)
Variable theX
String theGraph

String outwname = theGraph+"at"+num2str(theX)
outwname = CleanupName(outwname, 1)
extractGraphAtX(theX, theGraph, outwname)
end

Function extractGraphAtX(theX, theGraph, outwname)
Variable theX
String theGraph
String outwname

if (strlen(theGraph) == 0)
theGraph = WinName(0,1)
endif

String tlist = TraceNameList(theGraph, ";", 1)
Variable i
Variable ntraces = ItemsInList(tlist)
Variable pnt

Make/D/O/N=(ntraces) \$outwname
Wave outw = \$outwname

for (i = 0; i < ntraces; i += 1)
String oneTrace = StringFromList(i, tlist)
Wave w = TraceNameToWaveRef(theGraph, oneTrace)
Wave/Z xw = XWaveRefFromTrace(theGraph, oneTrace)
if (WaveExists(xw))
// assume monotonic X values
pnt = BinarySearch(xw, theX)
else
pnt = x2pnt(w,  theX)
endif
outw[i] = w[pnt]
endfor
end```

This function extractGraphAtXValue() extracts one Y value at a given X value from every trace on the named graph. If theGraph is "" then it works on the top graph window. It creates a wave with the name derived from the graph name and the X value.

The function extractGraphAtX() allows you to specify your own output wave name.

These functions will work on a mix of both XY and waveform traces.

Note that it does not check that all the traces have sufficient points- any short trace will contribute the value at the last point.

A nice enhancement would be to allow a filter string to select only traces with certain naming convention.

To get the Y values from a graph at the X value of the round (A) cursor in the top graph, you would use the function like this:

extractGraphAtXValue(hcsr(A), "")

This code will add a dynamic update- call SetupDynamicYatXforGraph() to add the A cursor to the graph, make a new graph with the output wave, and set up a window hook on the graph to update the output graph any time the cursor is moved.

```Function SetupDynamicYatXforGraph(theGraph)
String theGraph

if (strlen(theGraph) == 0)
theGraph = WinName(0,1)
endif

Cursor/H=2/L=0/P A \$(StringFromList(0, TraceNameList(theGraph, ";", 1))) 0

SetWindow \$theGraph hook(YatXCursorHook) = YatXCursorHook
String outwname = "DynamicYatX"+theGraph
SetWindow \$theGraph userdata(YatXDynamicName)=outwname
Make/N=0/D \$outwname
Display \$outwname
extractGraphAtX(hcsr(a), theGraph, outwname)
end

Function YatXCursorHook(s)
STRUCT WMWinHookStruct &s

String outwname = GetUserData(s.winName, "", "YatXDynamicName")
extractGraphAtX(hcsr(a), s.winName, outwname)
end```