DEV Community

Cover image for Scilab: A Beginner's Guide to Basic Commands II

Scilab: A Beginner's Guide to Basic Commands II

In this lesson, we'll be covering basic commands and operations in Scilab. Whether you're new to programming or just new to Scilab, this tutorial will provide you with the skills you need to get started. So let's dive in!

Printing Messages to the Console in Scilab

One of the most basic operations you'll need to know how to do in Scilab is printing messages to the console. This is often used for debugging or simply displaying output to the user.

To print a message to the console in Scilab, we use the disp function. Here's an example of how to use it:

--> disp('Hello, world!')
Hello, world!
Enter fullscreen mode Exit fullscreen mode

As you can see, the disp function takes a string argument, which is the message that you want to print. You can also use the disp function to print the values of variables:

--> x = 5
--> disp(x)
5
Enter fullscreen mode Exit fullscreen mode

In addition to strings and variables, the disp function can also be used to print the results of arithmetic operations:

--> disp(3 + 4)
7
--> disp(8 / 2)
4
Enter fullscreen mode Exit fullscreen mode

mprintf function

The mprintf function is a interfaced for the printf function, which is a standard C library function for printing formatted output to the console.

The mprintf function allows you to print to a memory buffer, rather than to the console. This is useful if you want to store the formatted output in a string or other data structure, rather than printing it directly to the console.

The syntax for using mprintfis similar to printf. It takes a format string and a variable number of arguments, and returns the number of characters written to the memory buffer. Here is an example of how to use mprintf:

// Example 1:
mprintf('At iteration %i, Result is:\nalpha=%f', 33, 0.535);
// Output: At iteration 33, Result is:
//         alpha=0.535000

// Example 2:
mprintf('%e ', [1; 2; 3]);
// Output: 1.000000e+00 2.000000e+00 3.000000e+00 

// Example 3:
mprintf('%d %d\n', [1, 2; 3, 4]);
// Output: 1 2
//         3 4
Enter fullscreen mode Exit fullscreen mode

In the first example, mprintf will print a string that includes the formatted values of the integers 33 and 0.535. The %i and %f format specifiers are used to specify that the corresponding arguments are integers and floating-point numbers, respectively.

In the second example, mprintf will print the elements of the matrix [1; 2; 3] with a %e format specifier, which specifies that the numbers should be printed in scientific notation.

In the third example, mprintf will print the elements of the matrix [1, 2; 3, 4] with %d format specifiers, which specify that the numbers should be printed as integers. The \n in the format string causes a newline to be printed after each row of the matrix.

The last part of this sub-serie of Beginner's Guide to Basic Commands will be about control structures. As you continue your course, you will have the opportunity to learn and practice more about these printing methods and see more examples of how to use them.

Top comments (0)

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆