Scripts

Just a series of Matlab commands.

In [1]:
a = 2;
a^20
sin(a)
a + 2 * factorial(13) / 1e9 + (2 + 2) * 2
ans =  1048576
ans =  0.90930
ans =  22.454

Functions

A much better option than scripts. A function with no arguments and no outputs is a script.

They usually leave in separate files (a file per function).

The file name must match the function name!

These notebooks, however, let me defined functions right here (for convenience).

Basics

In [2]:
function output = my_function(input1, input2)
    output = input1^2 + input2; % suppress with a semicolon (typically)
end

Function call, calling a function, with arguments gives the output value back to where the function was called from.

In [3]:
my_function(1, 2)
ans =  3
In [4]:
a = my_function(1, 2);
a
a =  3

Multiple outputs

Use square brackets if more than one output.

Most often just right two separate functions instead of one with two (or more) outputs.

Multiple output arguments are usually only usefull when two things are computed in a common procedure together anyway.

In [5]:
function [output1, output2] = my_function(input1, input2, input3)
    output1 = input3;
    a = input1 / input2;
    output2 = a;
end

Use square brackets to capture the values from the function.

In [6]:
[out1, out2] = my_function(1, 2, 3)
out1 =  3
out2 =  0.50000

If only one variable is captured, then it is the first.

In [7]:
a = my_function(1, 2, 3)
a =  3

You can directly ignore an output with a tilde $\verb|~|$

In [8]:
[~, b] = my_function(1, 2, 3)
b
~ % you can't use this as a variable, this is a special thing
b =  0.50000
b =  0.50000
parse error:

  syntax error

>>> ~ % you can't use this as a variable, this is a special thing
                                                                ^

An example of a multiple output in-built function when things are computed together.

In [9]:
v = rand(1, 10)
[max_item, index_of_max] = max(v)
v(index_of_max)
v =

 Columns 1 through 7:

   0.876034   0.053739   0.180703   0.616166   0.139028   0.612749   0.212664

 Columns 8 through 10:

   0.193215   0.454405   0.458286

max_item =  0.87603
index_of_max =  1
ans =  0.87603

Anonymous Functions

Are covered in a separate notebook.

Very important.

Commenting

Just use $\verb|%|$ to comment.

Or a pair of $\texttt{%\{}$ and $\texttt{%\}}$ to comment out an entire block of text/code.

In [10]:
% a single line comment
a = 2; % a comment after code ends on the line
%{
A block comment for a longer description.
%}

%{ Or to disable some code for now.
A = zeros(4, 4);
A(1, 2) = 7 - 14 * rand();
-abs(A);
%}

Printing Stuff Out

Just omit the semicolon

In [11]:
a = 2.0
sin(2.0)
my_function(1, 2, 3)
a =  2
ans =  0.90930
ans =  3

Use the $\texttt{disp}$ function

In [12]:
disp(a);
disp(my_function(1, 2, 3));
 2
 3

Use the $\texttt{fprintf}$ormatted

In [13]:
% fprintf('<format string>', var1, var2);
fprintf('My number, a, is equal to %d\n', a);
My number, a, is equal to 2

$\texttt{%d}$ - decimal/integer

$\texttt{%i}$ - integer

$\texttt{%f}$ - floating point, a "nubmer with a comma", like 2.43543534452 or $\pi$

$\texttt{%s}$ - string

In [14]:
fprintf('My name is %s and I''m %i years old\n', 'Robert', factorial(17));
My name is Robert and I'm 355687428096000 years old

$\texttt{\\n}$ is the newline character, it's typed as with a backslash and $\texttt{n}$, but it really is only one character

Don't forget it.

In [15]:
fprintf('a = %d', 2);
fprintf('b = %f', pi);
a = 2
b = 3.141593
a = 2b = 3.141593