FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Command Line Draft 2.0.1

GNU OCTAVE

GNU Octave is both a programming language and a command-line language interpreter. The peculiarity of GNU Octave is that it enables sequences of activities to be performed as if they were a single activity to be silently iterated over all elements of a given set of things (e.g. files, lines, numbers, ...).

This is not new to experienced command-line users and many other languages allow such a convenient conciseness.  However, GNU Octave language belongs to the category of array-programming oriented languages and this can be summarized by saying that the typical elementary object within GNU Octave is not a single element (a certain file, line, number, ...) but a collection (arrays) of such elements on which an activity is to be applied element by element without the need to explicitly tell GNU Octave to do so.

GNU Octave is included by default with most GNU/Linux distributions. The easiest way to experiment with it is to open its interactive intepreter:

$ octave -q
octave:1>

The text "octave:1>" is the prompt of the GNU Octave interpreter, ready to read your first (":1>") command. 

A sequence (an array) with the first ten natural numbers can be generated by typing 1:10 (meaning: from 1 to 10):

octave:1> 1:10
ans =

    1    2    3    4    5    6    7    8    9   10

Summing the first ten natural numbers can be easily done with the command sum :

octave:2> sum( 1:10 )
ans = 55

Iterating the sum by showing the partial cumulate sums is also straightforward:

octave:3> cumsum( 1:10 )
ans =

    1    3    6   10   15   21   28   36   45   55

A multiplication table can be generated by multiplying the first ten natural numbers each other, in all possible combinations. Instead of explicitly iterating over the array [1:10] , it is possible to let GNU Octave take care of the details:

octave:3> [1:10]'*[1:10]
ans =
     1     2     3     4     5     6     7     8     9    10
     2     4     6     8    10    12    14    16    18    20
     3     6     9    12    15    18    21    24    27    30
     4     8    12    16    20    24    28    32    36    40
     5    10    15    20    25    30    35    40    45    50
     6    12    18    24    30    36    42    48    54    60
     7    14    21    28    35    42    49    56    63    70
     8    16    24    32    40    48    56    64    72    80
     9    18    27    36    45    54    63    72    81    90
    10    20    30    40    50    60    70    80    90   100

 The GNU Octave programming language is largely compatible with the MATLAB language so that most of the commands and scripts you can write for GNU Octave can also be used as MATLAB language commands or scripts, and vice-versa. This compatibility could enable you to suggest users of non-free interpreters of MATLAB/GNU Octave languages to experiment free software alternatives.

Apart from directly using the interactive interpreter, one could also invoke GNU Octave by using a text editor to write a file and then passing it to the octave program.

GNU Octave scripts can be named anything but conventionally end with ".m". You can use any text editor to create this file -- Emacs, Vim, Gedit, or whatever your favorite is. A script could look like this:

a = sum( 1:10 );
disp( a );

In this example, we create a variable called a , which stores the result of the sum of all natural numbers between 1 and 10. It then uses the disp function to display the result, which should be 55. If we save this file as first.m, we can run it from the command line.

$ octave -q first.m
55

The GNU Octave program printed out "55", just like we expected. If we don't want to type "octave" in order to run the script, we can put this line:

#!/usr/bin/octave

at the start (be sure to use the correct path on your system), and do chmod +x first.m to make it executable. Then we type ./first.m to run it.

Of course, we can use GNU Octave to do more useful things. For example, we can look at all the files in the current directory to list only those whose extension is ".m".

filename = cellstr( ls( '*.m' ) );
fprintf( 1, 'file: %s\n', filename{:} )
$ octave first.m
file: octave-script.m
file: other-script.m

Here we use the ls function to silently invoke the ls command-line utility looking for all files with pattern "*.m". Since the result is a string, we split the string in rows by using the cellstr function so that the variable filename is assigned with an array of strings (more precisely, a cell-array whose elements are all strings). The final command tells GNU Octave what to print as output. In particular, here we ask GNU Octave to prefix each file name with the string "file: " followed by the file name ("%s") and by an endline ("\n").

Note that no explicit loops are needed.

More information about GNU Octave

The GNU Octave web site at http://www.gnu.org/software/octave/ contains a variety of information and documentation about the GNU Octave language and interpreter.

 

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.