MedCalc  # Scripts - Tables

In MedCalc script, a table is like a matrix but, unlike matrices, tables can contain both numbers and text. In addition, tables can be formatted.

Tables are primarily used for representation of data and results.

## How to create a table

A table is a workspace variable which is created with the Table(tname) command.

The following code creates a table with name t

Table(t);


After being created, the table t is still empty.

## How to put text and values in table

### Put values in individual cells

You can add, or change, the value of one cell of the table as follows:

Table(t);
t[1,1]="PI";   // Place the text PI in cell with row 1 and column 1
t[1,2]=3.14;   // Place the value 3.14 in cell with row 1 and column 2
?t;


This results in:


PI3.14


Complete columns and rows can be added using the AppendColumn, AppendRow, PrependColumn and PrependRow commands (see Table commands).

For example:

Table(a);
AppendRow(a,"Var","Mean");
AppendRow(a,"Weight",72.1);
AppendRow(a,"Height",1.68);
Print(a);


results in :


VarMean
Weight72.1
Height1.68


### Convert a matrix into a table

You can easily convert a matrix into a table by assigning the matrix to the table.

a=Magic(4);    // Create a matrix
Table(t);      // Create a table
t=a;           // Convert matrix a to table t
?"Matrix:\n";
?a;
?"Table:";
?t;


The output of this script is:

Matrix:

162313
511108
97612
414151

Table:

162313
511108
97612
414151



## Formatting a table

You can format a table, a single table cell, a column or a row, using the following commands:

Table(a);
AppendRow(a,"Var","Mean");
AppendRow(a,"Weight",72.1);
AppendRow(a,"Height",1.68);
FormatTable(a,"2"); // represent all numbers using 2 decimals.
Print(a);


results in :


VarMean
Weight72.10
Height1.68


## Transforming a table using spreadsheet functions

The following functions (with a few exceptions) can be used with tables:

When a numeric functions is applied to a table, it is only applied to the cells that contain a number. Likewise, when a string function is applied to a table, it is only applied to cells that contain text.

Table(a);
AppendRow(a,"Var","Mean");
AppendRow(a,"Weight",72.10);
AppendRow(a,"Height",1.68);
FormatTable(a,"2"); // represent all numbers using 2 decimals.
Print(a);
t=transpose(upper(a));
Print(t);


results in :


VarMean
Weight72.10
Height1.68

VARWEIGHTHEIGHT
MEAN72.101.68