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:
PI | 3.14 |
Add rows and columns
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 :
Var | Mean |
Weight | 72.1 |
Height | 1.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:
16 | 2 | 3 | 13 |
5 | 11 | 10 | 8 |
9 | 7 | 6 | 12 |
4 | 14 | 15 | 1 |
16 | 2 | 3 | 13 |
5 | 11 | 10 | 8 |
9 | 7 | 6 | 12 |
4 | 14 | 15 | 1 |
Formatting a table
You can format a table, a single table cell, a column or a row, using the following commands:
- FormatTable(tname,formatstring) formats a table.
- FormatTableCell(tname,row,column,formatstring) formats a table cell.
- FormatTableColumn(tname,column,formatstring) formats a table column.
- FormatTableRow(tname,row,formatstring) formats a table row.
Table(a); AppendRow(a,"Var","Mean"); AppendRow(a,"Weight",72.1); AppendRow(a,"Height",1.68); FormatTableRow(a,1,"shaded bold"); // apply shaded and bold style to row 1 FormatTable(a,"2"); // represent all numbers using 2 decimals. Print(a);
results in :
Var | Mean |
Weight | 72.10 |
Height | 1.68 |
Transforming a table using spreadsheet functions
The following functions (with a few exceptions) can be used with tables:
- Mathematical functions
- Trigonometric functions
- Hyperbolic functions
- Gamma function and related functions
- Engineering functions
- Matrix-like functions:
- NCOL(table) returns the number of columns of a table.
- NROW(table) returns the number of rows of a table.
- TRANSPOSE(table) returns the transpose of a table.
- String functions:
- LOWER(table) returns a table with all strings converted to lowercase
- UPPER(table) returns a table with all strings converted to
- PROPER(table) returns a table with the first character in each word converted to uppercase and the rest to lowercase
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); FormatTableRow(a,1,"shaded bold"); // apply shaded and bold style to row 1 FormatTable(a,"2"); // represent all numbers using 2 decimals. Print(a); t=transpose(upper(a)); Print(t);
results in :
Var | Mean |
Weight | 72.10 |
Height | 1.68 |
VAR | WEIGHT | HEIGHT |
MEAN | 72.10 | 1.68 |