Octave syntax

From Notes_Wiki

Home > Octave > Octave syntax

Declaring variables

Creating matrices

  • To define a matrix in Octave we have to enclose values in square brackets '[]'. Rows are seperated by semi-colons(;) and column values are seperated by commas(,). Hence to declare a 2x2 matrix 'p' with values 1,2,3 and 4 we can use
x = [1, 2; 3, 4]

Note: You can also declare different column values separated by space. But that is bad practice as that very often leads to ambiguous definitions.

  • We cannot define matrix with one or more dimension as zero by hand. We should call built-in functions like zeros(), eye() etc. to get matrices which have one or more dimension as zero.


Using ranges

  • To declare a matrix / array we can also use range notation. The syntax in range notation is `<base> : <increment> : <limit>'. For example to declare matrix `x=[1,3,5,7,9]' one can use range notation and write `x=[1:2:9]'.


Creating strings

  • To declare a string variable we have to enclose string value in either single quotes or double quotes. Like C or PHP in double quotes strings escape sequences like '\n', '\t', etc. can be used.

String values inside columns get concatenated. String values of different rows appear on different lines in output. For example `x=["a", "b"; "c", "d"]' get converted to

x= 
ab
cd


Creating structures

  • To declare a structure you can use normal "<parent_name>.<child_name>" syntax. For example
x.a.b=4  (creates struct as shown below)
x =
{
  a =
  {
    b =  4
  }

}


Creating cell arrays

Cell arrays can be used to store several variables of different size or type in one variable. Since '[' and ']' are used to create / manage matrices, we have to use '{' and '}' for creating / managing cell arrays. Example code that creates a cell array of size 1 by 2 is

octave:2> c = {"a string", rand(2, 2)}
c =

{
  [1,1] = a string
  [1,2] =

     0.96813   0.66620
     0.57733   0.69817

}


Creating global variables

In octave we can declare global variables by using global keyword. If we want to refer to same global variable inside function then we must declare variable as global inside function too, same as we do in PHP.


Creating persistent or static variables

We can declare static or persistent variables in octave using `persistent' or `static' keyword. Both are treated in exactly same way. Example of function that uses static variable is

function igotcalledtimes = igotcalledtimes()
static a=0;
a=a+1;
igotcalledtimes=a;
endfunction



Writing functions

Returning structures from functions

A sample function that returns structure is shown below

function y = f (x)
y.re = real (x);
y.im = imag (x);
endfunction


Managing cell arrays

Indexing cell arrays

Indexing cell arrays is explained with the help of examples:

  • All the rows of the first and third column of a cell array can be set to 0 with the following code
c{:, [1, 3]} = 0;

Note that operator `:' can be used to specify entire correct range. Hence one can use c{1, :} to print all values in first row of cell array. Also we can use c{:} to print all values of cell array and so on.


  • To access single element in cell array, one can use following syntax
element = c{1, 2};
  • To create another cell array from elements of existing cell array we can use
a = {1, rand(2, 2), "three"};
b = { a{ [1, 3] } }
      => b =
          {
            [1,1] =  1
            [1,2] = three
          }

We can also do above thing using '(' and ')' for indexing cell arrays. Whenever '(' and ')' are used for indexing cell arrays a new cell array containing chosen elements is returned. For example the above operation can also be done as:

a = {1, rand(2, 2), "three"};
b = a( [1, 3] )
     => b =
         {
            [1,1] =  1
            [1,2] = three
         }


Writing long lines of code

  • We can continue long lines of code with writing either '...' or '/' at end of line. For example,
 x = long_variable_name ...
         + longer_variable_name \
         - 42

We can also write comment after the continuation character as shown in next example

x = long_variable_name ...          # comment one
         + longer_variable_name \   # comment two
         - 42                       # last comment

Note that input that occurs between parenthesis can be continued to next line without using continuation marks


Home > Octave > Octave syntax