Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


table

create a table from variables

Syntax

t = table(var1, ..., varN)
t = table(m)
t = table(st)

t = table(..., Name, Value)

Arguments

var1, ..., varN

vector or matrix with the same number of rows.

Each var can be different type: double, boolean, string, datetime or duration

m

double, boolean, string, datetime or duration matrix

st

structure

Name, Value

'VariableNames': matrix of strings that contains the name of each column of t. Default value: ["Var1", ..., "VarN"]

'RowNames': indicator for reading the first column as row names

t

table object.

Description

table is a data type used to store heterogeneous data. Each column of the table represents a variable, named VarN (N: column number) by default. Each variable in a table can have different data type. They must all have the same number of rows. Variable names are unique.

Regarding the extraction and insertion of data, a table is like a matrix: t(i,j) where i and j are indices. In addition to indices, it is possible to use the names of variables and rows to access the data: t.Var1 or t("Var1") where "Var1" is a variable name; t("Row1", "Var1") where "Row1" is a row name and "Var1" is a variable name. It is also possible to mix indices and variable name or row name: t(1,["Var1", "Var3"]) or t("Row1", [1 3]). The data being generally heterogeneous, it will be neccesary to use {} or table to insert data. If they are of the same type, the [] will preferred.

Use the readtable function to create a table from a file. If data contains a timestamp, use the timeseries function instead.

t = table(var1, ..., varN) creates a table from the data variables var1, ..., varN. The variables can be column vector or matrix with the same number of rows. Each var1, ..., varN can be different type.

t = table(m) creates a table from a matrix m. This syntaxe corresponds to t = matrix2table(st).

t = table(st) creates a table from a structure st. This syntaxe corresponds to t = struct2table(st).

t = table(..., Name, Value) can be used to specify the variable names or the row names.

  • 'VariableNames': specify the name of variables. It is a row vector and the number of names must be equal the number of table variables. This data is stored in the properties of the table t in the 'VariableNames' field. For more information, see the Properties section below.

  • 'RowNames': specify the name of rows. The number of names must be equal the number of table rows. This data is stored in the properties of the table t in the 'RowNames' field. For more information, see the Properties section below.

Table Properties

t contains several properties used to describe the index and the variables.

These properties can be accessed/modified by using Properties field of t.

Here is the list of available fields in t.Properties:

  • Description: contains the description of the table. Default value: "".

  • VariableNames: contains the variable names of the table. Default value: ["Var1", ..., "VarN"].

  • VariableDescriptions: contains the variable descriptions of the table. One description by variable. Default value: "".

  • VariableUnits: contains the variable units of the table. Default value: "".

  • RowNames: contains the row names of the table. Default value: ""

Examples

t = table(var1, ..., varN) with var1, ..., varN are column vectors

Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"];
NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"];
Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2
NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; 
LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years

t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy)

t = table(var1, ..., varN, "VariableNames", Value)

Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"];
NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"];
Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2
NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; 
LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years

t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ...
    "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"])

t.Properties

// Add a description to the table
t.Properties.Description = "table of the 7 continents"

// Add variableunits to the table
t.Properties.VariableUnits(3) = "km2"
t.Properties.VariableUnits($) = "years"

t.Properties

t = table(var1, ..., varN, "VariableNames", Value, "RowNames", Value)

Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"];
NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"];
Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2
NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; 
LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years

t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ...
    "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"])

t.Row
t.Row = Code
t.Code = [];
t(["NA", "SA"], :)

t = table(NameContinent, Area, NumberCountry, LifeExpectancy, ...
    "VariableNames", ["NameContinent", "Area", "NumberCountry", "LifeExpectancy"], "RowNames", Code)

t.Properties.RowNames
t("NA", ["NameContinent", "NumberCountry"])

Access to data in the table

rand("seed", 0)
x = ["a"; "b"; "b"; "c"; "a"];
x1 = floor(rand(5,1)*5)-1.5;
x2 = -floor(rand(5,1)*5)+0.5;
t = table(x, x1, x2, "VariableNames", ["x", "x1", "x2"])
t.x1
t(["x1", "x2"])

t([1 3 5], 2)
t(6,:) = {"d", 2, -2}
t.x4 = (1:6)'

See also

History

VersionDescription
2024.0.0 Introduction in Scilab.
Report an issue
<< synchronize Timeseries/Table table2cell >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:15 CEST 2024