While programming we might need to display the values of our variables in the Scilab console for debug purposes or to output the final results of our script/algorithm.
The best way to display the values of our variables is by using the “mprintf()” function. This function is very similar with the C code “printf()”.
The format is a string of characters which defines how the data will be displayed in the Scilab console. The data to be displayed has a generic name instructions.
The instructions can be:
- constants (e.g.
- variables (e. g.
- mathematical operations (e.g.
var_1 + var_2)
The general structure of the format field is:
There are two mandatory fields that are needed for the function to work:
- percent sign (%): this informs the funtion that a formating sequence begins
- specifier: this describes what type of data will be displayed
The other fields, in square brackets , are optional.
In the table below is a complete description of each field of the
|[escape]||There are two escape sequences used by |
|[flag]||Flags are used to format the alignment of the displayed data or to add a prefix for octal and hexadecimal data types:|
|[width]||It is a positive integer number that defines how many character places are used for data display. By using |
|[.precision]||It is also a positive integer number that defines how many places are used for the decimal part of the displayed data|
|[specifier]||Keyword used to define the data type of the data to be displayed. Scilab supports the following data types:|
The Scilab function
mprintf is very similar to the C programming language
Now we are going through a series of example using the
mprintf function. Load them one by one in Scilab and try to guess how the data will be displayed in the console before executing the instruction.
1. Display of multiple values in different format on several lines
x1 = 18.25; mprintf('Float %f as:\n integer %i\n hexadecimal %x',x1, x1, x1)
2. Display of multiple values in different format on single line with tab spacing
x1 = 18.25; mprintf('Float %f as: integer %i\t hexadecimal %x',x1, x1, x1)
3. Float number displayed on 9 positions with 4 decimal numbers
x1 = 4/3; mprintf('%9.4f', x1)
4. Generate number list from 1 to 15 in decimal, octal and hexadecimal representation
for i=1:16 if i == 1 mprintf('Decimal\tOctal\tHexadecimal\n'); else mprintf('%d\t %#o\t %#X\n', i-1, i-1, i-1) end end
As you can see, the
mprintf function is very flexible, has a lot of setup parameters, making data display in the Scilab console quite easy and readable.
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!