Scilab is very powerful and versatile when working with data, especially in matrix format. As a Scilab developer you might need to write a script, for a particular algorithm, which needs to display data in the Scilab console in a table format.
The embedded Scilab function mprintf()
can do this easily and efficient.
For example let’s say that we need to display the table below:
Temperature [°C] | Temperature [K] | Temperature [°F] |
-40 | 233.15 | -40.00 |
-30 | 243.15 | -22.00 |
-20 | 253.15 | -4.00 |
-10 | 263.15 | 14.00 |
0 | 273.15 | 32.00 |
10 | 283.15 | 50.00 |
20 | 293.15 | 68.00 |
30 | 303.15 | 86.00 |
40 | 313.15 | 104.00 |
50 | 323.15 | 122.00 |
60 | 333.15 | 140.00 |
70 | 343.15 | 158.00 |
80 | 353.15 | 176.00 |
90 | 363.15 | 194.00 |
100 | 373.15 | 212.00 |
The table above shows the conversion of the temperature from degrees Celsius [°C] to Kelvin [K] and degrees Fahrenheit [°F].
To recall the formulas for temperature conversion, read the article Temperature.
The Scilab script which displays the table above in the console, using the mprintf()
function is:
clc T_degC = [-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100]; T_K = T_degC + 273.15; T_degF = T_degC * (9/5) + 32; mprintf("\nTemperature\tTemperature\tTemperature \n"); mprintf("[Celsius]\t[Kelvin]\t[Fahrenheit]\n"); for i=1:length(T_degC) mprintf("%d \t\t %.2f \t %.2f \n", T_degC(i), T_K(i), T_degF(i)); end
The first line calls the clc()
function to clear the Scilab console. We use it to be sure that we have a clean Scilab console before we display our table.
The vector variable T_degC
contains all the values of the temperature in °C, starting from -40 to 100, with increments of 10 °C. We could also generate this vector automatically by using the embedded Scilab function linspace()
:
-->linspace(-40,100,15) ans = column 1 to 11 - 40. - 30. - 20. - 10. 0. 10. 20. 30. 40. 50. 60. column 12 to 15 70. 80. 90. 100. -->
The variable T_K
contains all the temperature values in Kelvin. The variable T_degF
contains all the temperature values in degrees Fahrenheit. For both we use the appropriate temperature conversion formulas.
Our table needs a header which describes the content of each column. In our script we split the table header into two lines. First line contains the keyword Temperature
three times:
mprintf("\nTemperature\tTemperature\tTemperature \n");
The first escape sequence \n
starts a new line to display the table header. Each escape sequence \t
adds a horizontal tab between two Temperature
keywords. At the end we use another \n escape sequence to move the cursor at the beginning of the next line.
The following line adds the temperature unit for each of the Temperature
columns:
mprintf("[Celsius]\t[Kelvin]\t[Fahrenheit]\n");
We use the \t
escape sequence to add a horizontal tab between columns. At the end we add a \n
escape sequence to move the cursor to the next line.
The display of the data is done using a for
loop.
for i=1:length(T_degC) mprintf("%d \t\t %.2f \t %.2f \n", T_degC(i), T_K(i), T_degF(i)); end
With the instruction length(T_degC)
we calculate the number of values contained in the temperature vector.
At each iteration of the for
loop, we display a line in the Scilab console. Again we make sure to use \t
and \n
escape sequences to position the cursor in the right place before displaying the temperature value.
After running the script we get the following output in the Scilab console.
Temperature Temperature Temperature
[Celsius] [Kelvin] [Fahrenheit]
-40 233.15 -40.00
-30 243.15 -22.00
-20 253.15 -4.00
-10 263.15 14.00
0 273.15 32.00
10 283.15 50.00
20 293.15 68.00
30 303.15 86.00
40 313.15 104.00
50 323.15 122.00
60 333.15 140.00
70 343.15 158.00
80 353.15 176.00
90 363.15 194.00
100 373.15 212.00
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!