matrixbase::CopyFromWks

Description

Copies a specified range from a worksheet to a matrix.

Syntax

BOOL CopyFromWks( Worksheet & wksSource, int c1 = 0, int c2 = -1, int r1 = 0, int r2 = -1, DWORD dwCntrl = 0 )

Parameters

wksSource
[input] Source worksheet from which data is copied
c1
[input] Begining column index, default is 0 (0 based offset)
c2
[input] Ending column index, (inclusive) default -1 is GetNumCols -1 (0 based offset)
r1
[input] Begining row index, default is 0 (0 based offset)
r2
[input] Ending row index, (inclusive) default -1 is GetNumRows -1 (0 based offset)
dwCntrl
[input] Options defined in OC_Const.h : MCFW_CHECK_IGNORE_HIDDEN_ROW, etc.

Return

Returns TRUE on successful exit and FALSE on failure.

Examples

EX1

// Copy partial worksheet data into a matrix
void matrixbase_CopyFromWks_ex1()
{
    BOOL rc;
 
    Worksheet wks;
    wks.Create();
    Dataset myXDs(wks,0);
    Dataset myYDs(wks,1);
    String wksName=wks.GetPage().GetName();
 
    //******* Create sample data *****************
    myXDs.SetSize(4);
    myYDs.SetSize(4);
    myXDs[0]=0;  myYDs[0]=0.5;
    myXDs[1]=1;  myYDs[1]=1.5;
    myXDs[2]=2;  myYDs[2]=2.5;
    myXDs[3]=3;  myYDs[3]=3.5;    
 
    matrix<double> mat1 = {
        {0,  0,  0,  0},
        {0,  0,  0,  0}
    };
    //******** End of Sample Data Creation *******
    
    MatrixPage MatPg1;
    MatPg1.Create("Origin");
    MatrixLayer MatLy1 = MatPg1.Layers(0);
    Matrix Mat1(MatLy1);
    Mat1 = mat1;
    printf("  The original matrix is %s(Size=%dx%d).\n",
      Mat1.GetName(),Mat1.GetNumRows(),Mat1.GetNumCols());
 
    MatrixPage MatPg2;
    MatPg2.Create("Origin");
    MatrixLayer MatLy2 = MatPg2.Layers(0);
    Matrix Mat2(MatLy2);
 
    rc = mat1.CopyFromWks(wks); //Copies the whole wks to mat1
    Mat2 = mat1;
 
    if(!rc) 
        printf("  Error: CopyFromWks failed.\n");
    else
        printf("  %s has been copied into %s(Size=%dx%d).\n",
          wksName,Mat2.GetName(),Mat2.GetNumRows(),Mat2.GetNumCols());
        // Result matrix by this sample program will be:
    //  {0,  0.5}
    //  {1,  1.5}
    //  {2,  2.5}
    //  {3,  3.5}

    MatrixPage MatPg3;
    MatPg3.Create("Origin");
    MatrixLayer MatLy3 = MatPg3.Layers(0);
    Matrix Mat3(MatLy3);
    
    rc = mat1.CopyFromWks(wks,1,1,2,3); //Copies a part of wks to mat1
    Mat3 = mat1;
 
    if(!rc) 
        printf("  Error: CopyFromWks failed.\n");
    else
        printf("  %s has been copied into %s(Size=%dx%d).\n",
          wksName,Mat3.GetName(),Mat3.GetNumRows(),Mat3.GetNumCols());
    // Result matrix:
    //  {2.5}
    //  {3.5}
}

Remark

Copies a specified range from a worksheet to a matrix dynamically resizing the matrix as needed.

See Also

header to Include

origin.h