All programming languages are either:
Programming languages can be classified as either:
A computing language & interactive environment for algorithm development, data visualisation, data analysis and numerical computation, e.g.
Matlab has a range of potential applications in biomedical engineering, e.g.:
Matlab alternatives include Octave, Freemat & Scilab
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)
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
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);
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]
Matlab provides a number of built-in functions specifically for array operations:
max(d) - maximum value of dmin(d) - minimum value of dsum(d) - sum of values of dmean(d) - mean of values of dstd(d) - standard deviation of dpt1 = [0 1 1]; pt2 = [1 1 0];dot(pt1,pt2) - dot productcross(pt1,pt2) - cross productMatlab values/variables can take the following data types:
1.234 (single, double)3 (int8, int16, int32, int64)’a’ (char)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
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');
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 graphxlabel('Angle in radians’); - Adds x-axis labelylabel('Sine’); - Adds y-axis labelaxis([0 pi 0 1]); - Defines figure boundariesIt 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];
There are built-in Matlab functions for initialising arrays:
a = zeros(2,2) - matrix of zerosb = ones(2,2) - matrix of onesc = eye(2,2) - identity matrixNote the difference between matrix operations and element-wise operations:
d = b * c - d is a matrix of onese = b .* c - e is the identity matrixFor complex series of operations script m-files can be very useful. Just make sure you save your script with the .m extension!
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
Scripts do not always work first time, however to help you debug your scripts, Matlab provides two useful tools: