# Identify Point in Matrix Subrange

Igor allows you to display a subrange of a multi-dimensional wave as a waveform or XY trace in a graph. For example, you can display a column of a 2D wave as a trace:

Make/O/N=(5,5) mat = p + 10*q
Display mat[][2]    // Display all rows of column 2

In the case of a 2D wave, the Y coordinates for a trace can come from any of the following:

Display mat[][2]        // All rows of one column
Display mat[1,3][2]     // Range of rows of one column
Display mat[0,4;2][2]       // Range of rows of one column with increment
Display mat[2][]        // All columns of one row
Display mat[2][1,3]     // Range of columns of one row
Display mat[2][0,4;2]       // Range of columns of one row with increment

The X coordinates for a trace can come from any of the above or, in the case of a waveform display, are calculated from the 2D wave's row or column scaling.

The attached experiment demonstrates how to determine the row and column indices of a data point of a subrange display using the included GetMatrixTraceIndices function.

Here is the function itself:
// GetMatrixTraceIndices(graphName, traceName, dataIndex, wantY, matrixRow, matrixColumn)
//
// traceName is the name of a trace whose X or Y coordinates come from a (matrix) 2D wave.
//
// dataIndex is the row index (when displaying a column of a matrix) or the column index
// (when displaying a column of a matrix) of the matrix element supplying the data
// for a point in the trace. This is the value returned by pcsr(<cursor>) when the
// specified <cursor> is on the an element of the trace.
//
// Returns via matrixRow and matrixColumn the indices of the specified point in the matrix.
//
// The function result is zero if everything went right or non-zero in the event of an error.
//
// Calling this when the trace in question does not come from a matrix returns -1.
// See demo below.
Function GetMatrixTraceIndices(graphName, traceName, dataIndex, wantY, matrixRow, matrixColumn)
String graphName            // Graph name or "" for top graph
String traceName            // Trace name, e.g. "mat" or "mat#1"
Variable wantY              // 0 to get indices for X coordinate, 1 to get indices for Y coordinate
Variable dataIndex          // See explanation above
Variable& matrixRow     // Output
Variable& matrixColumn      // Output

String info = TraceInfo(graphName, traceName, 0)
if (strlen(info) == 0)
return -1               // No such trace
endif

Variable isXY = WaveExists(XWaveRefFromTrace(graphName, traceName))
if (wantY==0 && !isXY)
wantY = 1               // This is a waveform plot so X values come from Y wave
endif

String rangeStr
if (wantY)
Wave w = TraceNameToWaveRef(graphName, traceName)
rangeStr = StringByKey("YRANGE", info)
else
Wave w = XWaveRefFromTrace(graphName, traceName)
rangeStr = StringByKey("XRANGE", info)
endif

if (WaveDims(w) != 2)
return -1               // Not a matrix
endif

String rowStr, columnStr, temp
String regExp = "(\[)(.*)(\])(\[)(.*)(\])"
SplitString /E=regExp rangeStr, temp, rowStr, temp, temp, columnStr, temp
// Print rowStr, columnStr          // For debugging

Variable displayingColumn = 1       // True if trace data comes from matrix column, not matrix row
if (CmpStr(columnStr,"*") == 0)
displayingColumn = 0            // e.g., Display mat[2][*]  // Display all columns of row 2
endif
if (strsearch(columnStr,",",0) >= 0)
displayingColumn = 0            // e.g., Display mat[2][1,3]    // Display columns 1 to 3 of row 2
endif

if (displayingColumn)
// mat[*][<column>], mat[<firstRow>,<lastRow>][<column>] or mat[<firstRow>,<lastRow>;<increment>][<column>]
matrixRow = dataIndex
matrixColumn = str2num(columnStr)
else                                    // !displayingColumn
// mat[<row>][*], mat[<row>][<firstColumn>,<lastColumn>] or mat[<row>][<firstColumn>,<lastColumn>;<increment>]
matrixRow = str2num(rowStr)
matrixColumn = dataIndex
endif

return 0
End

Forum

Support

Gallery