While programming in Scilab, during program design phase, we need to print result in the Scilab console mainly for debug purposes. For this objective we can use the Scilab built-in disp
function, which is easy to understand and quite flexible.
The syntax of the disp
function:
disp(x1,[x2,...xn])
In the simplest form, the disp
function prints in the Scilab console only the value of the variables x1, x2, ... xN
, without printing also the variable name.
The variables to be displayed can be of type:
- scalar
- matrix
- string
- list
If there are several variables to be displayed, the first one shown in the Scilab console is the last in the function arguments (LIFO – Last In First Out).
Here are several examples on how to use the disp
function:
1. Display a scalar
-->var_dec = 147; -->disp(var_dec) 147. -->
2. Display a string
-->var_str = 'Some random string'; -->disp(var_str) Some random string -->
3. Display a string with special characters (quotes)
-->var_str_spe = 'The movie ''Random movie name'' was good'; -->disp(var_str_spe) The movie 'Random movie name' was good -->
4. Display a random matrix
-->var_mtx = rand(5,3); -->disp(var_mtx) 0.2320748 0.3076091 0.2922267 0.2312237 0.9329616 0.5664249 0.2164633 0.2146008 0.4826472 0.8833888 0.312642 0.3321719 0.6525135 0.3616361 0.5935095 -->
5. Display multiple variables with different type (scalar and string)
-->var_mass = 34; -->disp('The mass of the body is ', var_mass, ' kg.') kg. 34. The mass of the body is -->disp(' .kg', var_mass, 'The mass of the body is ') The mass of the body is 34. .kg -->
Pay attention to the order of the display (LIFO).
6. Display a Scilab list variable
-->var_list = list([],[],[]); -->var_list(1) = 'Scilab'; -->var_list(2) = 6.0; -->var_list(3) = [12 15]; -->disp(var_list) (1) Scilab (2) 6. (3) 12. 15. -->
The disp
function is easy to use and very useful in the early stages of of the script/program design.
A more versatile function for data display is the mprintf
function. About it we’ll discuss in another article.
For any questions, observations and queries regarding this article, use the comment form below.
Don’t forget to Like, Share and Subscribe!