Scilab allows to store different type of variables (numeric, strings, etc.) in the same structure. For this type of data handling we can use lists, cells and structures. These type of variables (objects) can contain real data, complex, strings, matrices or other.
Lists
In Scilab a list can be defined using the list();
function:
-->list_variable=list(142, "someText", [1 4;6 2], %T, %s^2+1) list_variable = list_variable(1) 142. list_variable(2) someText list_variable(3) 1. 4. 6. 2. list_variable(4) T list_variable(5) 2 1 + s -->
In the example above the list variable named list_variable
contains 5 types of data:
- real
- string
- matrix
- boolean
- polynomial
Lists are useful when we want to store different data types for the same object. For example we would like to store the power ratings [HP@rpm] for different engines:
Engine | Power [HP] | Engine Speed [rpm] |
Audi | 150 | 5000 |
Ford | 152 | 5200 |
Mazda | 159 | 4800 |
In can easily store all the data in only one variable of type list:
-->EnginesOutput = list(["Audi" "Ford" "Mazda"],[150 152 159], [5000 5200 4800]) EnginesOutput = EnginesOutput(1) !Audi Ford Mazda ! EnginesOutput(2) 150. 152. 159. EnginesOutput(3) 5000. 5200. 4800. -->
Another, more flexible way to store and manipulate different types of data is by using typed lists. The advantage of the typed list, compared to the simple list, is that you can extract the information you need by using symbolic names. The symbolic names are defined together with the data.
For example the table above can be store as typed list using the function tlist()
. In this case we can define also the header of the columns “Engine”, “Power [HP]” and “Engine Speed [rot/min]” as follows:
-->EnginesOutput = tlist(["id" "Engine" "Power" "EngineSpeed"], ["Audi" "Ford" "Mazda"],[150 152 159], [5000 5200 4800]) EnginesOutput = EnginesOutput(1) !id Engine Power EngineSpeed ! EnginesOutput(2) !Audi Ford Mazda ! EnginesOutput(3) 150. 152. 159. EnginesOutput(4) 5000. 5200. 4800. -->
The advantage is that you can access the data by using the column header within the table. For example to check the power of the Audi engine, this command must be used:
-->EnginesOutput.Power(1) ans = 150. -->
The difference between list()
and tlist()
, regarding data extraction is highlighted in the table below:
list();
-->EnginesOutput(2)(1) ans = 150. -->
tlist();
-->EnginesOutput.Power(1) ans = 150. -->
Cells
This type of variable emulates the same cell type variable from Matlab®. The cell variables within Scilab can be used to store multidimensional arrays. A cell can be defined in Scilab as follows:
-->cellOne = makecell([2,1],rand(2,2),rand(2,2)) cellOne = !{2x2 constant} ! ! ! !{2x2 constant} ! -->
The variable cellOne
contains two matrices of dimension 2 x 2, with random numbers. In order to display them we must use the .entries
command after the cell name:
-->cellOne.entries ans = ans(1) 0.6783956 0.0258710 0.3320095 0.5174468 ans(2) 0.3916873 0.5064435 0.2413538 0.4236102 -->
Please note that any variable types can be used in the same cell. For example you can define a cell which contains a matrix, a string and a Boolean value:
-->cellTwo = makecell([1,3],"John Doe", [1 2;3 4], %T) cellTwo = !"John Doe" {2x2 constant} %t ! -->
Structures
The structure variable within Scilab emulates the same type of variable as in Matlab®. This type of variable let you assign different. In fact a variable of type structure groups together several other variables named members. The separation between structure variable and members is done using the dot character “.”
:
structure.member1
structure.member2
………………………..
structure.memberN
The members can have different data types, they can be treated as different variables. For example in the same structure you can store real, complex variables, matrices, strings, polynomials and so on.
-->structure.member1 = 187 structure = member1: 187 -->structure.member2 = "String" structure = member1: 187 member2: "String" -->structure.member3 = rand(2,2) structure = member1: 187 member2: "String" member3: [2x2 constant] -->
You have also the possibility to define a multidimensional structure. That means that you arrange the structure as a matrix. Check out this example:
structMatrix(1,1).member1 = "firstElement_member1" structMatrix(2,1).member1 = "secondElement_member1" structMatrix(1,1).member2 = "firstElement_member2" structMatrix(2,1).member2 = "secondElement_member2" -->structMatrix.member1 ans = ans(1) firstElement_member1 ans(2) secondElement_member1 -->
By the end of this article you should be able to define almost any type of variables, objects that are used in Scilab.
In the following articles we will learn how to manipulate these variables, how to extract the data and information that we need.
For any questions, observations and queries regarding Scilab variables use the comment form below.
Don’t forget to Like, Share and Subscribe!