# 1.6.3.4 Virtual Matrix

You can construct a virtual matrix from a worksheet window. Pick separate data ranges from the worksheet for X, Y, Z data of the virtual matrix. If you do not specify X and Y data, it will automatically use default data. The following code shows how to construct a virtual matrix from an active worksheet window, and then plot this virtual matrix on a graph.

// before running, make sure there is active worksheet window with data.
// For example, new a worksheet window, import XYZ Random Gaussian.dat from
// Origin folder Samples\Matrix Conversion and Gridding subfolder to worksheet.
Worksheet wks = Project.ActiveLayer();

int r1, r2;
int c1 = 0, c2 = 2;
wks.GetBounds(r1, c1, r2, c2);

// construct a data range object only with Z data, X and Y data will be auto
// assigned.
DataRange dr;
dr.Add("Z", wks, r1, c1, r2, c2);

MatrixObject mo;
mo.Attach(dr);

int nRows = mo.GetNumRows();
int nCols = mo.GetNumCols();

// get the default x, y range
double xmin, xmax, ymin, ymax;
mo.GetXY(xmin, ymin, xmax, ymax);

GraphPage gp;
gp.Create("CONTOUR");
GraphLayer gl = gp.Layers(0);

gl.AddPlot(mo, IDM_PLOT_CONTOUR);
gl.Rescale();

mo.Detach();

If you want to assign X and Y data then the data should be monotone. The following example shows how to construct a virtual matrix with an XYZ data range.

// Assume the active layer is a worksheet with 5 columns of data.
Worksheet wks = Project.ActiveLayer();

// Get min and max row indices for columns 0 to 4.
int r1, r2, c1 = 0, c2 = 4;
wks.GetBounds(r1, c1, r2, c2);

// Create a data range object with XYZ data.
DataRange dr;
dr.Add("X", wks, 0, 1, 0, c2); // First row except the first cell
dr.Add("Y", wks, 1, 0, r2, 0); // First column except the first cell
dr.Add("Z", wks, 1, 1, r2, c2);

MatrixObject mo;
mo.Attach(dr);