Creating tables manually with values

Before creating any tables, all fields must be defined centrally as MetaData object like the example below:

// define all fields in MetaData
var md = MetaData.CreateEmptyMasterData();
MetaData.AddNewField(md, "category", FieldType.TextAttribute);
MetaData.AddNewField(md, "animal", FieldType.TextAttribute);
MetaData.AddNewField(md, "year", FieldType.IntegerAttribute);
MetaData.AddNewField(md, "day", FieldType.DateAttribute);
MetaData.AddNewField(md, "population", FieldType.KeyFigure);
MetaData.AddNewField(md, "weight_kg", FieldType.KeyFigure);

As the second step, the fields of the table (i.e. table structure) must be defined as a TableFields object:

// Define structure (i.e. fields) of first table
var tfields1 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields1, "animal");
TableFields.AddNewField(tfields1, "population");
TableFields.AddNewField(tfields1, "year");

Elementary table functions for creating tablesIn order to see all elementary table functions that create tables just write “” in the text file in Visual Studio. You will see the list of all table functions that begin with “Create”. This is the extremely helpful intellisense help of Visual Studio. For each function you select you can also see a short explanation of the function as intellisense text.

Having defined MetaData and table structure we can now use the table functions for creating tables.

CreateTableWithElements_A(TableFields TFields, params object[] TableElements)
Creates a table with given field structure (TFields) and values. Reads values row by row; value order must match field order.

// Create table with values for each field (A)
var table1 = MatrixTable.CreateTableWithElements_A(tfields1,
"tiger", 2010, 2500.0,
"lion", 2010, 5200.0,
"leopard", 2010, 3300.0
// display table in immediate window
System.Diagnostics.Debug.WriteLine("tbl1 = \n" + table1);
// view table
MatrixTable.View_MatrixTable(table1, "Table: population(animal, year)");

Table: population(animal, year)

Note that the values must be entered with the same order of fields in table. In a table, fields are always registered in following order:

  1. Text attributes
  2. Numeric attributes
  3. Key figures

Display ordered fields in immediate windowIn order to see how individual fields are ordered within each field type you can call the ColumnNames property of TableFields:

// display ordered fields in immediate window
System.Diagnostics.Debug.WriteLine("Ordered fields in table1: " + tfields1.ColumnNames);

Let’s create a second table with the numeric attribute day as defined in MetaData:

Create a second table with numeric attribute "day"

// Define structure of second table with day
var tfields2 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields2, "animal");
TableFields.AddNewField(tfields2, "day");
TableFields.AddNewField(tfields2, "population");
// create a second table with fields animal, day and population
var table2 = MatrixTable.CreateTableWithElements_A(tfields2,
"tiger", DateFunctions.DayToNumber(20, 10, 2010), 1500.0,
"lion", DateFunctions.DayToNumber(22, 10, 2010), 3500.0,
"leopard", DateFunctions.DayToNumber(25, 10, 2010), 4500.0
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table2 = \n" + table2);
// view table in GridViewer
MatrixTable.View_MatrixTable(table2, "Table: population(animal, day)");

Table: population(animal, day)

CreateTableWithElements_B(MetaData md, TextVector ColumnNames, params object[] TableElements)
create table without TableFieldsDifference to version A is (CreateTableWithElements_A) the separate step for defining the structure of table (TableFields object) can be skipped with this table function. The field names (ColumnNames) must be entered in correct order, otherwise function generates an error (order of fields: text attributes, numeric attributes, key figures).

// create table without TableFields
var table3 = MatrixTable.CreateTableWithElements_B(md,
TextVector.CreateVectorWithElements("animal", "year", "population"),
"tiger", 2010, 1500.0,
"lion", 2011, 3500.0,
"leopard", 2012, 4500.0
System.Diagnostics.Debug.WriteLine("table3 = \n" + table3);

Fields entered in wrong order generate runtime errorFollowing code generates error because the numeric attribute year is entered before text attribute animal.

// try to create table with wrong field order
// numeric attribute "year" before text attribute "animal"
    table3 = MatrixTable.CreateTableWithElements_B(md,
    TextVector.CreateVectorWithElements("year", "animal", "population"),
    "tiger", 2010, 1500.0,
    "lion", 2011, 3500.0);
catch(Exception ex){
    System.Diagnostics.Debug.WriteLine("Error message: ", ex.Message);

CreateTableWithMatrices_A(TableFields TblFields, TextMatrix TextAttribValues,NumMatrix NumAttribValues, KeyMatrix KeyFigValues)
Generating tables with value matricesTables can also be created with three value matrices, each representing the values for text attributes, numeric attributes and key figures.

Field type and order is important: Matrix columns must match the field order defined in TableFields. All value matrices must have the same number of rows unless a matrix is empty. An empty matrix must be entered for a field type without any fields in it.

// Define structure (i.e. fields) of table
var tfields4 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields4, "category");
TableFields.AddNewField(tfields4, "animal");
TableFields.AddNewField(tfields4, "population");
TableFields.AddNewField(tfields4, "year");
// create 3x2 text matrix</span>
TextMatrix tM = TextMatrix.CreateMatrixWithElements(3,2,
    "mammals", "tiger",
    "mammals", "lion",
    "mammals", "leopard");
// create 3x1 numeric matrix
NumMatrix nM = NumMatrix.CreateMatrixWithElements(3, 1, 2010, 2011, 2012);
// create 3x1 key figure matrix
KeyMatrix kM = KeyMatrix.CreateMatrixWithElements(3, 1, 1200.0, 2500.0, 3500.0);
// create table with matrices representing field values
var table4 = MatrixTable.CreateTableWithMatrices_A(tfields4, tM, nM, kM);
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table4 = \n" + table4);

An empty matrix must be entered as input if there is no field in the corresponding field type. In the example below, there is no numeric attribute in the table.

Create table with an empty matrix for a field type

// define table structure without a numeric attribute
var tfields5 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields5, "animal");
TableFields.AddNewField(tfields5, "population");
// generate value matrices for each field type
tM = TextMatrix.CreateMatrixWithElements(2, 1, "tiger", "lion");
nM = NumMatrix.CreateEmptyMatrix();
kM = KeyMatrix.CreateMatrixWithElements(2, 1, 1200.0, 2500.0);
// create table with matrices; empty numeric matrix
var table5 = MatrixTable.CreateTableWithMatrices_A(tfields5, tM, nM, kM);
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table5 = \n" + table5);

CreateTableWithMatrices_B(MetaData md, TextVector ColumnNames, TextMatrix TextAttribValues, NumMatrix NumAttribValues, KeyMatrix KeyFigValues)
Create table with matrices BThis function is similar to its version A above (CreateTableWithMatrices_A). The only difference is that the additional step of defining the table structure (TableFields) can be skipped. The table fields can directly be defined within the function as input parameters (ColumnNames). Field names must be entered in correct order as in function CreateTableWithElements_B() explained above.

// generate value matrices for each field type
// create 3x2 text matrix
tM = TextMatrix.CreateMatrixWithElements(3, 2,
    "mammals", "tiger",
    "mammals", "lion",
    "mammals", "leopard");
// create 3x1 numeric matrix
nM = NumMatrix.CreateMatrixWithElements(3, 1, 2010, 2011, 2012);
// create 3x1 key figure matrix
kM = KeyMatrix.CreateMatrixWithElements(3, 1, 1200.0, 2500.0, 3500.0);
table4 = MatrixTable.CreateTableWithMatrices_B(md,
TextVector.CreateVectorWithElements("category", "animal", "year", "population"),
tM, nM, kM);
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table4 = \n" + table4);

CreateEmptyTable(TableFields TFields)
Create empty table to add rows subsequentlySometimes, creating an empty table (i.e. row-less) might be desirable to add rows (i.e. entries) subsequently.

// Define structure of table
var tfields6 = TableFields.CreateEmptyTableFields(md);
TableFields.AddNewField(tfields6, "animal");
TableFields.AddNewField(tfields6, "year");
TableFields.AddNewField(tfields6, "population");
// create row-less table
var table6 = MatrixTable.CreateEmptyTable(tfields6);
// add first row
var trow = TableRow.CreateTableRowWithElements(tfields6, "tiger", 2010, 1200.0);
table6 = MatrixTable.AddRowToTable(table6, trow);
// add second row
trow = TableRow.CreateTableRowWithElements(tfields6, "lion", 2011, 2200.0);
table6 = MatrixTable.AddRowToTable(table6, trow);
// display table in immediate window
System.Diagnostics.Debug.WriteLine("table6 = \n" + table6);

CloneTable(MatrixTable tbl)
Clone tableA table can also be created by cloning an existing table. Note that cloned tables are independent and separate variables; changes in one table doesn’t affect the other one. See the example below:

// clone existing table
var table7 = MatrixTable.CloneTable(table6);
// set population = 111 in first row of table7
MatrixTable.SetKeyFigValue(table7, "population", 0, 111.0);
// display both tables in immediate window
System.Diagnostics.Debug.WriteLine("table6 = \n" + table6);
System.Diagnostics.Debug.WriteLine("table7 = \n" + table7);
Copyright secured by Digiprove © 2012 Tunc Ali Kütükcüoglu

Leave a Reply