Getting Started


Getting Started

matlab



Programming languages

All programming languages are either:

  1. Interpreted: Code translated into machine-readable instructions as the program is run, by the interpreter. e.g. Perl, Python, Javascript, Matlab
  2. Compiled: This involves two steps, first compilation converts code into machine-readable instructions; execution then runs these instructions. e.g. C, C++, Java, Matlab

Three types of programming language

Programming languages can be classified as either:

  1. Procedural: Programs consist of sequence of instructions that operate on data. e.g. C, Fortran, Perl, Matlab
  2. Object-orientated: Programs consist of interacting objects containing data and operations. e.g. C++, Java, Matlab
  3. Declarative: programs consist of statements about the problem or how it can be solved. e.g. Prolog, ML

What is Matlab?

A computing language & interactive environment for algorithm development, data visualisation, data analysis and numerical computation, e.g.

  • Visualise data
  • Perform mathematical computations
  • Write computer programs

Matlab has a range of potential applications in biomedical engineering, e.g.:

  • Image Reconstruction
  • Biophysical Modelling

Matlab alternatives include Octave, Freemat & Scilab

The Matlab environment

Variables and functions

Variables can be created by assigning values to them in the command window, e.g.

a = 1
y = 3
c = a+y
d = a^y
e = y/2 - a*4

Inbuilt functions of Matlab can also be used, e.g.

sin(pi/4)
z = tan(a)

Arrays

Variables can be scalar values or arrays of values:

c = 2;
d = [1 3 4 2 5];
e = [2, 2, 3, 3, 2];

Matlab allows array operations to be performed on each array element separately:

c^3
d+3
d.^3
d.^e
d.*e
Creating Arrays

Arrays can also be created using the shorthand colon operator:

x = 1:100;
y = 20:5:50;
z = 0:0.1:pi;

Or using the linspace command

x = linspace(1,100,100);
y = linspace(20,50,7);
Accessing Arrays

Individual array elements can be accessed using round brackets:

d = [1 3 4 2 5];
e = [2 2 3 3 2];
d(3)
e(1:3)

It is possible to assign values to the array elements in the same way:

d(3) = 0
f = e(1)
g = [d(1) e(2) d(3) 5 10]
Built in array functions

Matlab provides a number of built-in functions specifically for array operations:

  • max(d) - maximum value of d
  • min(d) - minimum value of d
  • sum(d) - sum of values of d
  • mean(d) - mean of values of d
  • std(d) - standard deviation of d
  • pt1 = [0 1 1]; pt2 = [1 1 0];
  • dot(pt1,pt2) - dot product
  • cross(pt1,pt2) - cross product

Data Types

Matlab values/variables can take the following data types:

  • Floating Point, e.g. 1.234 (single, double)
  • Integer, e.g. 3 (int8, int16, int32, int64)
  • Character, e.g. ’a’ (char)
  • Boolean, e.g. true (logical)

It is possible to find out the data type of a MatLab variable using the whos command:

a = [1 2 3];
b = ‘x’;
c = false;
whos a b c

Loading and saving data

There are a number of ways of loading and saving data in Matlab, e.g. dlmwrite writes text files with different delimiters between the array values:

f = [1 2 3 4 9 8 7 6 5];
dlmwrite('test.txt',f,'\t');

The load and save commands can be used to store (parts of) of your workspace for later use:

d = load('test.txt');
e = d / 2;
save('newdata.mat', 'd', 'e');
save('alldata.mat');
clear
load('alldata.mat');

Starting Programming

Visualisation

Graph Plotting

2D plots can be produced using the plot command, e.g.

x = 0:0.1:2*pi;
y = sin(x);
plot(x,y,'-b');

It is possible to add annotations or the graph:

  • title(‘My Sine Plot); - Adds a title to graph
  • xlabel('Angle in radians’); - Adds x-axis label
  • ylabel('Sine’); - Adds y-axis label
  • axis([0 pi 0 1]); - Defines figure boundaries
Matrices

It is very easy to define matrices in MatLab using 2D arrays:

a = [1 2; 3 4];
b = [2, 4;
 1, 3];

Access matrix elements using round brackets:

a(1,2)
b(1,:)

Assign matrix elements in the same way:

b(2,2) = 3;
a(:,2) = [4; 5];
Matrix operations

There are built-in Matlab functions for initialising arrays:

  • a = zeros(2,2) - matrix of zeros
  • b = ones(2,2) - matrix of ones
  • c = eye(2,2) - identity matrix

Note the difference between matrix operations and element-wise operations:

  • d = b * c - d is a matrix of ones
  • e = b .* c - e is the identity matrix

Creating and running scripts

For complex series of operations script m-files can be very useful. Just make sure you save your script with the .m extension!

Adding Comments

When writing more complex scripts, it is a good idea to comment code. Any text on a line after a % sign will not be interpreted by MatLab. e.g.

x = [1 2 3]
% comment here
y = x.^2
Code analyser and debugger

Scripts do not always work first time, however to help you debug your scripts, Matlab provides two useful tools:

  • Code analyser
  • Debugger


return  link
Written by Tobias Whetton