Compose-2000: Initial OML Script
- Build an OML script using a simple assignment and arithmetic.
- Use flow control in OML.
- Write custom functions.
Simple Assignments and Arithmetic
-
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.
-
Click Run to display the following in the OML
Command Window:
Flow Control
-
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 than1
boo
is equal to5
, otherwise (else)boo
is equal to6
. Adding an end statement closes the logical structure. -
Click Run to display the output for
boo
: -
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
-
In OML, the basic structure of a function is:
function funcname() statement to execute end
-
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] tomat
.x
is assigned tomat
, then displaysmat
. To call the function simple, addfoo()
afterend
, as shown below: -
Click Run to evaluate your script and see the output of
foo
. -
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: