2.2.4.9.10 DataRange::GetData

Description

Get the data from indexed subrange get data from all subrange if nIndex is negative.


It extracts data from the object.


It extracts matrix data from DataRange.

Syntax

BOOL GetData( vectorbase * pData, int nIndex, DWORD dwRules2 = 0 )


int GetData( DWORD dwRules, int nIndex, DWORD * pouidAux, string * pstrDescriptive, vectorbase * pv, vector * pvIndep = NULL, matrix * pmMultiIndep = NULL, vector<string> * pstrFactors = NULL, vector * pvWeights = NULL, vector * pvErrDep = NULL, vector * pvErrIndep = NULL, Worksheet * pwksData = NULL, vector * pvYIndep = NULL, vector<int> * pvintRowsInSource = NULL, STGETDATAOUTPUTINFO * pstOutInfo = NULL, DWORD dwRules2 = 0 )


int GetData( DWORD dwRules, int nIndex, vector<int> & vIndicesIn, vector & vMainOut, vector & vIndepOut, vector & vYIndepOut = NULL )


int GetData( matrixbase & m, DWORD dwRules = 0, int index = 0, DWORD dwRules2 = 0 )


int GetData(STDRGETDATAARGS & gdInfoOut, STGETDATAOUTPUTINFO * pstOutInfo = NULL)

Parameters

pData
[output] it receives the data in range
nIndex
[input] the index of data


dwRules
[input]the optional bits from the DRR_ enumeration.
nIndex
[input]the index of the data. DataRange can reference multiple sets of data. Index starts from 0. See AddInput for an example.
pouidAux
[output] if supplied, it receives the dataplot UID stored in the DataRange object.
pstrDescriptive
[input]not used
pv
[output]it receives the main data values.
pvIndep
[output]it receives the X-independent data if DRR_GET_DEPENDENT or DRR_GET_Z_DEPENDENT rules bits are passed in.
pmMultiIndep
[output]it receives the (potentially) multiple X-independent data if DRR_GET_DEPENDENT and DRR_ONE_DEP_MULTIINDEP bits are passed in
(appropriate for Multiple Regression).
pstrFactors
[output]it receives the values of factors for each factor data that correspond to the values
returned into pv. The size of the returned vector is equal to the total number of factors.
pvWeights
[output]it receives the weight values
pvErrDep
[output]it receives the dependent error values
pvErrIndep
[output]it receives the independent error values
pwksData
[output] it is initialized to the Worksheet object to which the main data belongs.
pvYIndep
[output] it receives the Y-independent data. Used only if DRR_GET_Z_DEPENDENT rules bit is supplied.
pvintRowsInSource
[output] it receives the row indices in the source
pstOutInfo
[output]a pointer which points to output structure STGETDATAOUTPUTINFO. Keep it as NULL if the output is not necessary. STGETDATAOUTPUTINFO is defined in OC_types.h
dwRules2
bits from the DRR2_ enumeration.


dwRules
[input]the rules for extracting data from DRR_ enumeration.
nIndex
[input]the data index
vIndicesIn
[input]the row indices that need to be extracted
vMainOut
[output]the main data
vIndepOut
[output]the X independent data
vYIndepOut
[output]the Y-independent data (used only if DRR_GET_Z_DEPENDENT supplied in dwRules, which means XYZ data)


m
[output]the matrixbase object which will receive the matrix data.
dwRules
[input]not used, must be 0.
index
[input]data index in case DataRange refers to multiple matrices.


gdInfoOut
[modify]a STDRGETDATAARGS structure which includes all parameters except output structure STGETDATAOUTPUTINFO. STDRGETDATAARGS is defined in Range.h:
struct STDRGETDATAARGS 
{
	int nIndex;
	DWORD dwRules;
	DWORD dwRules2;
	DWORD *pouidAux;
	string *pstrDescriptive;
	vectorbase *pv;
	vector *pvIndep;
	matrix *pmMultiIndep;
	vector<string> *pstrFactors;
	vector *pvWeights;
	vector *pvErrDep;
	vector *pvErrIndep;
	Worksheet *pwksData;
	vector *pvYIndep;
	vector<int> *pvintRowsInSource;
};
pstOutInfo
[output]a pointer which points to output structure STGETDATAOUTPUTINFO. Keep it as NULL if the output is not necessary. STGETDATAOUTPUTINFO is defined in OC_types.h:
struct STGETDATAOUTPUTINFO
{
    int nCountNegativeErrorValuesFound;
    int nCountZeroErrorValuesFound;
    int nCountMissingErrorValuesFound;
};

Return

true if successfully, else false.


If it succeeds, it returns the index of the row (if the DRR_BY_ROWS rules bit passed in) or the column from which the main data is drawn, otherwise it returns a negative number.


If it succeeds, it returns the index of the row (if the DRR_BY_ROWS rules bit passed in) or the column from which the main data is drawn, otherwise it returns a negative number.


1 if success.


If it succeeds, it returns the index of the row (if the DRR_BY_ROWS rules bit passed in) or the column from which the main data is drawn, otherwise it returns a negative number.

Examples

EX1

// This example assumes a worksheet with two columns of data is active
void DataRange_GetData_Ex1()
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
        return;
    
    DataRange dr;
    dr.Add(wks, 0, "A");
    dr.Add();
    dr.Add(wks, 1, "B");    
    
    vector vA, vB, vAll;
    dr.GetData(&vA, 0); // get the data from Column A
    dr.GetData(&vB, 2); // get the data from Column B
    dr.GetData(&vAll, -1);
}


EX2

// This example assumes a worksheet with one column of data is active
void DataRange_GetData_Ex2(int nXCol = 0)
{
    Worksheet wks = Project.ActiveLayer();
    if( wks )
    {                
        DataRange dr;
        dr.Add(wks, nXCol, "X");
        
        vector vX;
        DWORD dwPlotID;
        dr.GetData(DRR_GET_MISSING | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vX);
        vX *= 10.0;
        dr.SetData(vX);
    }
}

EX3

// costruct DataRange from Project.ActiveLayer()
void DataRange_GetData_Ex3(string strX = "A", string strY = "B")
{
    // For this example, a workbook with X and Y column data is assumed to be active.
    Worksheet wks = Project.ActiveLayer();
    if(!wks)
    {
        out_str("err, no wks");
        return;
    }
    
    // construct a tree with data from worksheet
    Tree tr;
    if( construct_one_data_range(tr, wks, strX, strY) )    
    {
        out_tree(tr);

        // create the DataRange object from the tree.
        DataRange dr;
        if(dr.Create(tr))
        {
            // Call GetData to copy the xy data into vectors vx and vy
            vector vx, vy;
            DWORD dwPlotID; // this is to receive data plot UID if present
            if(dr.GetData( DRR_GET_MISSING |  DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vy, &vx) < 0)
            {
                out_str("failed to make data copy from data range");
                return;
            }
            
            // now we have made a copy of xy data into vectors vx and vy
            // so we can do analysis on the data, for example:
            double xmin, xmax, ymin, ymax;
            vx.GetMinMax(xmin, xmax);
            vy.GetMinMax(ymin, ymax);
        
        }
        
        // no need to call dr.Destroy().

    }
    // dr is destroyed when go out of scope
}

EX4

// This example assumes a worksheet with three columns of data is active
void DataRange_GetData_Ex4(int nXCol = 0, int nYCol = 1, int nZCol = 2)
{ 
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
        out_str("error, no active worksheet");
        return;
    }                    
            
    DataRange drIn;
    drIn.Add("X", wks, 0, nXCol, -1, nXCol);
    drIn.Add("Y", wks, 0, nYCol, -1, nYCol);
    drIn.Add("Z", wks, 0, nZCol, -1, nZCol);

    vector vX, vY, vZ;
    DWORD dwRules = DRR_GET_MISSING | DRR_GET_Z_DEPENDENT | DRR_NO_FACTORS;
    DWORD dwPlotID;
    // Use this overloaded version of GetData to find out how many rows of XYZ data
    drIn.GetData(dwRules, 0, &dwPlotID, NULL, &vZ, &vX, NULL, NULL, NULL, NULL, NULL, NULL, &vY);
    int iSizeIn = vX.GetSize();
    int iSizeOut = iSizeIn/2 + 1;
    vector<int> vIndicesIn;
    vIndicesIn.Data(0, iSizeIn - 1, 2);
    // Use this overloaded version of GetData with vIndicesIn to extract odd rows to new columns
    drIn.GetData(dwRules, 0, vIndicesIn, vZ, vX, vY);

    string strColDesigs = wks.GetColDesignations();
    nXCol = wks.AddCol("XOut");
    nYCol = wks.AddCol("YOut");
    nZCol = wks.AddCol("ZOut");
    strColDesigs += "XYZ";
    wks.SetColDesignations(strColDesigs);

    DataRange drOut;
    drOut.Add("X", wks, 0, nXCol, -1, nXCol);
    drOut.Add("Y", wks, 0, nYCol, -1, nYCol);
    drOut.Add("Z", wks, 0, nZCol, -1, nZCol);
    drOut.SetData(&vZ, &vY, &vX);
    
}

EX5

// This example assumes 1 or more columns of main data, 1 column of factor or grouping data, and 1 column of weight data(see sample data)
void DataRange_GetData_Ex5(int nXColStart = 0, int nXColEnd = 1, int nFCol = 2, int nWCol = 3)
{
    Worksheet wks = Project.ActiveLayer();
    if( wks )
    {
        DWORD dwRules = DRR_GET_MISSING | DRR_BAD_WEIGHT_TREATMENT;
        DataRange dr;
        dr.Add("X", wks, 0, nXColStart, -1, nXColEnd); // Main data
        dr.Add("F", wks, 0, nFCol, -1, nFCol); // Factor data
        dr.Add("W", wks, 0, nWCol, -1, nWCol); // Weight data
        int nNumData = dr.GetNumData(dwRules);
        vector<string> vstrFactors;
        vector vData, vW;
        double dN, dMean, dMax, dMin;
        for( int ii = 0; ii < nNumData; ii++ )
        {
            int nCol = dr.GetData(dwRules, ii, NULL, NULL, &vData, NULL, NULL, &vstrFactors, &vW);
            if ( 0 <= nCol )
            {
                vData *= vW;
                dN = vData.GetSize();
                vData.Sum(dMean);
                dMean /= dN;
                vData.GetMinMax(dMin, dMax);
                printf("\nColumn = %s, Factor = %s\nMean = %g\nMinimum = %g\nMaximum = %g\n", 
                        wks.Columns(nCol).GetName(), vstrFactors[0], dMean, dMin, dMax);
            }
        }
    }
}


EX6

// This example will create a new worksheet, the first two columns will be filled with 10 rows of random data,
// then even rows are extracted to the last two columns.
void DataRange_GetData_Ex6()
{
    Worksheet wks;
    wks.Create();
    int nXCol,nYCol;
    nXCol=0;
    nYCol=1;
    if( wks )
    {
        while(wks.DeleteCol(0));
        
        wks.AddCol("A");
        wks.AddCol("B");
 
        string strX,strY;
        strX="A";
        strY="B";
        double rr;
        for(int j=0;j<2;j++)
        {
            for (int i=0;i<10;i++)
            {
                   rr=rnd();
                   wks.SetCell(i,j,rr*100);
            }
        }
        DataRange drIn;
        drIn.Add("X", wks, 0, nXCol, -1, nXCol);
        drIn.Add("Y", wks, 0, nYCol, -1, nYCol);

        vector vX, vY;
        DWORD dwRules = DRR_GET_MISSING | DRR_GET_DEPENDENT | DRR_NO_FACTORS;
        DWORD dwPlotID;
        // use this overloaded version of GetData to find out how many rows
        drIn.GetData(dwRules, 0, &dwPlotID, NULL, &vY, &vX);

        int iSizeIn = vX.GetSize();
        int iSizeOut = iSizeIn/2 + 1;
        vector<int> vIndicesIn;
        vIndicesIn.Data(1, iSizeIn - 1, 2);
        // use this overloaded version of GetData with vIndicesIn to extract even rows to new columns
        drIn.GetData(dwRules, 0, vIndicesIn, vY, vX, NULL);

        string strColDesigs = wks.GetColDesignations();
        out_str(strColDesigs);
        nXCol = wks.AddCol();
        nYCol = wks.AddCol();
        strColDesigs += "XY";
        wks.SetColDesignations(strColDesigs);

        DataRange drOut;
        drOut.Add("X", wks, 0, nXCol, -1, nXCol);
        drOut.Add("Y", wks, 0, nYCol, -1, nYCol);
        drOut.SetData(&vY, &vX);
    }
}

EX7

// This example assumes a worksheet with three columns of data is active
void DataRange_GetData_Ex7()
{
    Worksheet wks;
    wks.Create();
    int nXCol,nYCol,nZCol;
    nXCol=0;
    nYCol=1;
    nZCol=2;
    
    if( wks )
    {
        while(wks.DeleteCol(0));
        
        wks.AddCol("A");
        wks.AddCol("B");
        wks.AddCol("C");
        double rr;
        for(int i=0;i<3;i++)
        {
            for (int j=0;j<10;j++)
            {
                   rr=rnd();
                   wks.SetCell(j,i,rr*100);
            }
        }
            
        DataRange drIn;
        drIn.Add("X", wks, 0, nXCol, -1, nXCol);
        drIn.Add("Y", wks, 0, nYCol, -1, nYCol);
        drIn.Add("Z", wks, 0, nZCol, -1, nZCol);

        vector vX, vY, vZ;
        DWORD dwRules = DRR_GET_MISSING | DRR_GET_Z_DEPENDENT | DRR_NO_FACTORS;
        DWORD dwPlotID;
        // Use this overloaded version of GetData to find out how many rows of XYZ data
        drIn.GetData(dwRules, 0, &dwPlotID, NULL, &vZ, &vX, NULL, NULL, NULL, NULL, NULL, NULL, &vY);
        int iSizeIn = vX.GetSize();
        int iSizeOut = iSizeIn/2 + 1;
        vector<int> vIndicesIn;
        vIndicesIn.Data(0, iSizeIn - 1, 2);
        // Use this overloaded version of GetData with vIndicesIn to extract odd rows to new columns
        drIn.GetData(dwRules, 0, vIndicesIn, vZ, vX, vY);

        string strColDesigs = wks.GetColDesignations();
        nXCol = wks.AddCol();
        nYCol = wks.AddCol();
        nZCol = wks.AddCol();
        strColDesigs += "XYZ";
        wks.SetColDesignations(strColDesigs);

        DataRange drOut;
        drOut.Add("X", wks, 0, nXCol, -1, nXCol);
        drOut.Add("Y", wks, 0, nYCol, -1, nYCol);
        drOut.Add("Z", wks, 0, nZCol, -1, nZCol);
        drOut.SetData(&vZ, &vY, &vX);
    }
}


EX8

// For this example to run, have two matrices, MBook1 and MBook2, and put some data into MBook2.
void DataRange_GetData_Ex8()
{
    DataRange dr;
    string strRange;
    strRange = "[MBook1]MSheet1!1";
    // Initialize the DataRange object:
    int nn = dr.AddInput(strRange);
    if (nn < 0)
        return;        // failed. Perhaps MBook1 does not exist.

    matrix mm;
    Matrix mat2("MBook2");
    if ( dr.GetData(mm) <= 0 )
    {
        out_str("Failed to get matrix data");
        return;
    }
    // Set the result into mat2:                
    mat2 = mm;
 
    return;
}

EX9

//This example will get the data from the worksheet and do some operation on the data, 
//then put the data to another three columns.
void DataRange_GetData_Ex9()
{
   //Creat a worksheet with XYZ columns and fill with data.
	Worksheet wks;
	wks.Create("origin");
	wks.AddCol();
	wks.SetColDesignations("XYZ");
	for (int ii=0; ii<20; ii++)
	{
		wks.SetCell(ii,0,ii);
		wks.SetCell(ii,1,ii+1);
		wks.SetCell(ii,2,ii*2);
	}
	
    //Get XYZ data.
	DataRange dr;
	dr.Add(wks, 0, "X");
	dr.Add(wks, 1, "Y");
	dr.Add(wks, 2, "Z");
	
	STDRGETDATAARGS gd;
	vector vX,vY,vZ;
	DWORD dwPlotID;
	gd.nIndex = 0;
	gd.pv = &vZ;
	gd.pvIndep = &vX;
	gd.pvYIndep = &vY;
	gd.dwRules = DRR_GET_MISSING |  DRR_GET_Z_DEPENDENT | DRR_NO_FACTORS;
	gd.pouidAux = &dwPlotID;
	
    //To get data from Worksheet XYZ columns to vX, vY, vZ vector.
	dr.GetData(gd);
	
	vX*=10.0;
	vY+=20.0;
	vZ-=30.0;

    //Add XYZ columns in the worksheet.	
	string strColDesigs = wks.GetColDesignations();
    int nXCol = wks.AddCol();
    int nYCol = wks.AddCol();
    int nZCol = wks.AddCol();
    wks.SetColDesignations(strColDesigs);
    
    //Set data in the new columns.
    DataRange drOut;
    drOut.Add(wks, nXCol, "X");
    drOut.Add(wks, nYCol, "Y");
    drOut.Add(wks, nZCol, "Z");
    
    drOut.SetData(&vZ, &vY, &vX);
	
}

EX10

//This example will get the data from the worksheet selection, include label area
void GetDataFromSelection()
{
	Worksheet wks = Project.ActiveLayer();
	if( !wks ) 
		return;
	
	Grid gg;
	int iRegions;	
	vector<int> vr1, vc1, vr2, vc2;	
	if( gg.Attach(wks) ) 
		iRegions = gg.GetSelection(vr1, vc1, vr2, vc2, true);
	
	if( 0 == iRegions )    
	{
		printf("No range selected in %s!", wks.GetName()); 
		return; 
	}		
	
	vector<string> vsData;
	for(int ii = 0 ; ii < iRegions; ii++) 
	{
		DataRange dr;
		dr.Add("Range", wks, vr1[ii], vc1[ii], vr2[ii], vc2[ii]); 	
		dr.GetData(vsData, 0);
	}
	out_int("Data Size=", vsData.GetSize());
	out_str(vsData[0]);
}

Remark

See Also

Curve::Curve,curvebase::AttachX,DataRange::GetMaskedData,DataRange::GetMissingData,DataRange::GetNumData,DataRange::SetData

Header to Include

origin.h