Display data in Scilab using the mprintf() function

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.

How to install Scilab

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()”.

Syntax

mprintf(format, instructions)

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. 2)
  • variables (e. g. var_1)
  • mathematical operations (e.g. 2+3, var_1 + var_2)

The general structure of the format field is:

Scilab mprintf format structure

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 mprintf function.

Field Description
[escape] There are two escape sequences used by mprintf in Scilab:
\n – Signals the start of a new line for display
\t – Horizontal tab, moves the start of the display towards right with a predefined number of spaces
[flag] Flags are used to format the alignment of the displayed data or to add a prefix for octal and hexadecimal data types:

- (minus sign) – Aligns the displayed data to the left side; the flag has effect on used with [width] field

+ (plus sign) – Forces the display of the sign for both negative and positive numbers

# (hash) – If used with specifiers (%o, %x and %X) will add a prefix before the data: 0x for %x, 0X for %X and 0 for %o

0 (zero) – When the [width] field is specified will replace left empty spaces with 0 (zeros)

[width] It is a positive integer number that defines how many character places are used for data display. By using 0 before the width number we fill the empty spaces with zeros
[.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:
%c – diaplays a unique character
%s – displays a string of characters
%d – displays an integer decimal number with sign
%i – displays an integer decimal number with sign
%u – displays an integer decimal number without sign
%f – displays a real number with sign
%g – displays a real number with sign in a short format (less decimal places)
%e – displays a real number with sign in exponential format
%x – displays a hexadecimal number (only unsigned integers)Instead of %e or %x we can use %E and %X, the only difference is that the string characters are going to be upper case letters.

The Scilab function mprintf is very similar to the C programming language printf function.

C programming printf function syntax

Image: C programming printf() function example

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.

Examples

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!

One Response

  1. Dave

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh