Command Line Arguments in Scilab

Command-line arguments are a common way to parameterize execution of programs. For example, scilab -f script.sce uses -f and script.sce arguments to the scilab program.

// The main function in Scilab is not explicitly defined.
// The code starts executing from the top of the script.

// In Scilab, we use the 'argn' function to get the number of arguments
// and the 'argv' function to get the arguments themselves.

// Get all arguments including the program name
argsWithProg = argv();

// Get arguments without the program name (skip the first argument)
argsWithoutProg = argsWithProg(2:$);

// You can get individual args with normal indexing.
// Note: Scilab uses 1-based indexing
if size(argsWithProg, '*') >= 3 then
    arg = argsWithProg(3);
else
    arg = [];
end

// Print the results
disp(argsWithProg);
disp(argsWithoutProg);
disp(arg);

To experiment with command-line arguments in Scilab, you can save this script to a file (e.g., command_line_arguments.sce) and run it from the command line:

$ scilab -nw -f command_line_arguments.sce a b c d

This will display:

["scilab", "-nw", "-f", "command_line_arguments.sce", "a", "b", "c", "d"]
["a", "b", "c", "d"]
c

Note that in Scilab, the program name and all flags (like -nw and -f) are included in the arguments list. The -nw flag is used to run Scilab in non-window mode, which is useful for command-line operations.

Scilab doesn’t have a direct equivalent to building a standalone executable like in compiled languages. Instead, you typically run Scilab scripts directly or use them in the Scilab environment.

Next, we’ll look at more advanced command-line processing techniques in Scilab.