DataRange::AddInput

Description

Add data to a DataRange object from named columns in a workbook.

Syntax

int AddInput( LPCSTR lpcszIndep, LPCSTR lpcszDep = NULL, LPCSTR lpcszWeight = NULL, LPCSTR lpcszFactors = NULL, LPCSTR lpcszErrDep = NULL, LPCSTR lpcszErrIndep = NULL, DWORD dwPlotUid = 0, LPCSTR lpcszZDep = NULL, LPCSTR lpcszSubjects = NULL )

Parameters

lpcszIndep
[input] the range string of the independent data column, like "[Book1]Sheet1!Col(A)"
lpcszDep
[input] the range string of the dependent data column, or second independent data if lpcszZDep is supplied
lpcszWeight
[input] the range string of the weights column
lpcszFactors
[input] the range string of the factors column
lpcszErrDep
[input] the range string of the independent error column
lpcszErrIndep
[input] the range string of the independent error column
dwPlotUid
[input] the UID of the data plot
lpcszZDep
[input] the range string of the dependent z data column
lpcszSubjects
[input] the range string of the subjects column

Return

The total number of ranges added (including separators).

Examples

EX1

//Add multiple xy subranges from named columns in a workbook to DataRange Object. 

void DataRange_AddInput_Ex1()
{
    Worksheet wks;
    wks.Create("Origin");
    if( !wks )
        return; 
 
    while(wks.Columns(0))
        wks.DeleteCol(0);                    
    Column colA(wks, wks.AddCol("A"));
    Column colB(wks, wks.AddCol("B"));
    Column colC(wks, wks.AddCol("C"));
    Column colD(wks, wks.AddCol("D"));
 
    for(int ii=0;ii<wks.GetNumCols();ii++)
    {
        for (int jj=0;jj<10;jj++)
        {
               double rr=rnd();
               wks.SetCell(jj, ii,rr*100);
        }
    }
 
    // Get range string of columns in workbook
    string strX, strY;
    colA.GetRangeString(strX);
    colB.GetRangeString(strY);
 
    // Add a set of columns to DataRange dr. Index will be 0 for this range.
    DataRange dr;
    int nn = dr.AddInput(strX, strY);
    out_int("The number of data columns added, including separator S = ", nn);
 
    // Add another set of columns to dr. Index will be 1 for this range. 
    colC.GetRangeString(strX);
    colD.GetRangeString(strY);
    nn = dr.AddInput(strX, strY);
    out_int("The number of data columns added, including separator S = ", nn);
 
    // Copy data associated with index=0 of dr into vectors using GetData
    DWORD dwPlotID; // This is to retrieve data plot UID if present
    vector vX, vY;
    DWORD dwRules = DRR_GET_MISSING | DRR_GET_DEPENDENT | DRR_NO_FACTORS;
    int nIndex = 0;
    nn = dr.GetData( dwRules, nIndex, &dwPlotID, NULL, &vY, &vX);
    out_int("Index of Y column = ", nn);
 
    // 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);
    printf("xmin = %g\nxmax = %g\nymin = %g\nymax = %g\n", xmin, xmax, ymin, ymax);
 
    // Copy the next set of data, associated with index=1 of dr into vectors using GetData
    vector     vX1, vY1;
    nIndex = 1;
    nn = dr.GetData( dwRules, nIndex, &dwPlotID, NULL, &vY1, &vX1);
    out_int("Index of Y column = ", nn);
 
    // Now we have made a copy of XY data into vectors vX1 and vY1
    // so we can do analysis on the data...for example:
    vX1.GetMinMax(xmin, xmax);
    vY1.GetMinMax(ymin, ymax);
    printf("xmin = %g\nxmax = %g\nymin = %g\nymax = %g\n", xmin, xmax, ymin, ymax);
}

EX2

//Get XYZ data from source worksheet ,  then put the data to the destination worksheet. 
void DataRange_AddInput_Ex2()
{
    // create two worksheet and fill one of the worksheet with data
    Worksheet wks;
    wks.Create("Origin");                
    
    int nCols = 3;
    int nRows = 10;
    wks.SetSize(nRows, nCols);
    
    for(int ii=0; ii<nCols; ii++)
    {
        for (int jj=0; jj<nRows; jj++)
        {
               wks.SetCell(jj, ii, rnd()*100);
        }
    }
        
    // Get names of columns in workbook    
    Column colX(wks, 0);
    Column colY(wks, 1);
    Column colZ(wks, 2);
    
    string strX, strY, strZ;
    colX.GetRangeString(strX);
    colY.GetRangeString(strY);
    colZ.GetRangeString(strZ);

    DataRange dr;
    // With XYZ data the dependent data is the last argument rather than the second argument (as is the case for XY data)
    int nn = dr.AddInput(strX, strY, NULL, NULL, NULL, NULL, 0, strZ);
    out_int("nn = ", nn);

    Worksheet wksDest;
    wksDest.Create("Origin");
    wksDest.SetSize(nRows, nCols);
    
    string strBookDest = wksDest.GetPage().GetName(); 
    Dataset dsX(strBookDest, 0); // Attaches to column 0 on active worksheet of Book named strBookDest
    Dataset dsY(strBookDest, 1); // Attaches to column 1 on active worksheet of Book named strBookDest
    Dataset dsZ(strBookDest, 2); // Attaches to column 2 on active worksheet of Book named strBookDest

    if( !dsX || !dsY || !dsZ )
    {
        error_report("Invalid destinaton!");
        return;
    }

    // fill the datasets with values. 
    // Note: GetData returns the dependent data in the fifth argument, and the independent data in the sixth and last arguments.
    DWORD dwRules = DRR_GET_Z_DEPENDENT | DRR_GET_MISSING| DRR_NO_FACTORS;
    nn = dr.GetData(dwRules, 0, NULL, NULL, &dsZ, &dsX, NULL, NULL, NULL, NULL, NULL, NULL, &dsY);
    out_int("\tnn = ", nn);                
}

EX3

// This shows how to associate a DataRange object with a data plot.
void DataRange_AddInput_Ex3()
{
    GraphLayer gl = Project.ActiveLayer();
    if( !gl )
    {
        printf("Please prepare a graph with plots to continue the sample");
        return;
    }
    
    DataPlot dp = gl.DataPlots(-1);
    if( !dp )
        return;    
    
    DataPlotStrings dpString;
    dp.GetPlotType(&dpString); 
    string strX = dpString.szIndepRange;
    string strY = dpString.szDepRange;
    
    uint nPlotUID = dp.GetUID(TRUE);
    out_int("Data Plot UID: ", nPlotUID);
    
    // Add range data to dr associated with specified plot UID 
    DataRange dr;
    dr.AddInput(strX, strY, NULL, NULL, NULL, NULL, nPlotUID);

    DWORD dwPlotUID;
    string strDescriptive;
    vector vY;
    dr.GetData(DRR_GET_DEPENDENT, 0, &dwPlotUID, &strDescriptive, &vY);
    printf("The Data Plot UID stored in the DataRange object: %d\n", dwPlotUID);
    
    //Please check two printed plot uid info if same                 
}

Remark

Add data to a DataRange object from named columns in a workbook.

See Also

DataRange::Add, DataRange::SetRange

header to Include

origin.h