Compose-2000: First OML Script

This tutorial will explain how to:
  • Build an OML script using a simple assignment and arithmetic.
  • Use flow control in OML.
  • Write custom functions.

Simple Assignments and Arithmetic

  1. To build your first OML script, add the following code in the Editor:
    a = 1 + 3;
    disp ('1 + 3 equals')
    disp(a) 
    
    disp('matrix multiply')
    b1 = [ 1 2; 3 4];
    b2 = [4 -2;-10 0];
    M1 = b1*b2;
    disp('b1 * b2 equals ')
    disp(M1) 
    
    disp('matrix dot multiply')
    M2 = b1.*b2;
    disp('b1 .* b2 equals')
    disp(M2)
    
    disp('matrix inversion')
    M3 = inv(b1);
    disp('inverse of b1 equals ')
    disp(M3)

    This script assigns variable a (a scalar) and b1, b2 (matrices). It then performs simple assignment and arithmetic functions (add, multiply, dot multiply, and inverse).

    The output is printed in the OML Command Window using the disp function.

  2. Click Run to display the following in the OML Command Window:

Flow Control

  1. In OML, the if statement accepts logical operations to affect what gets executed next. For example, if you add this code to the OML script:
    a = 100;
    if a > 1
          boo = 2;
    else
          boo = 3;
    end
    disp('boo equals ')
    disp(boo)

    Compose reads if a is greater than 1 boo is equal to 5, otherwise (else) boo is equal to 6. Adding an end statement closes the logical structure.

  2. Click Run to display the output for boo:
  3. OML also supports switch/case/otherwise flow control. It’s recommended that you use switch flow control when you need to consider multiple situations in the script, as shown in the example below:
    value = sign(ceil(randn));
    switch value      
          case -1
                disp('value is -1')
          case 0
                disp('value is 0')      
          case 1            
                disp('value is 1')      
          otherwise
                disp('otherwise situation')
    end

    sign(ceil(randn)) picks a value from -1, 1, or 0. Results printed in the OML Command Window may be different when running this script:

Writing Custom Functions

  1. In OML, the basic structure of a function is:
    function funcname()
          statement to execute
    end
  2. If you want to add a function to your OML script in the Editor window, add the following code:
    function foo()
        disp('Inside foo');
        mat = [1,2,3;4,5,6];
        x = mat;
        disp(x);
    end
    foo()

    This code first declares the function foo, then displays a message (Inside foo) to notify that the function has been called. It then assigns the matrix [1,2,3;4,5,6] to mat. x is assigned to mat, then displays mat. To call the function simple, add foo() after end, as shown below:

  3. Click Run to evaluate your script and see the output of foo.
  4. Below is an example of a function that contains a variable number of inputs, in which case you can use the varargin function:
    function out = pad(varargin)
          if nargin == 1
                option = 'zeros';
          elseif nargin == 2
                option = varargin{2};
          else
                error('invalid number of input argument')
          end
          M = varargin{1};
    
          [r,c] = size(M);
          if strcmp(option,'zeros')
                out = zeros(r+2,c+2);
                out(2:end-1,2:end-1) = M;
          elseif strcmp(option,'ones')
                out = ones(r+2,c+2);
                out(2:end-1,2:end-1) = M;
          else
                error('invalid option')
          end
    end

    This function pads zeros or ones 'around' the input matrix. The number of input argument could be one or two. The example below contains an input to this function, with the results below it: