![Page 1: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/1.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
CFCS1
Matlab Programming
Miles Osborne
School of Informatics
University of Edinburgh
February 23, 2010
Miles Osborne CFCS1 1
![Page 2: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/2.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
1 Data Types
2 Expressions
3 Control Loops
4 Functions
5 Input-Output
Miles Osborne CFCS1 2
![Page 3: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/3.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Data Types
Literals: 10, 10.5, -3 etc.
Booleans: 0 or 1
Vectors: (see class).
Matrices: (see class).
Strings: ’hello I am a string’
Miles Osborne CFCS1 3
![Page 4: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/4.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Expressions
Here are some example expressions:
octave-2.9.18:1> 1
ans = 1
octave-2.9.18:2> 1 + 1
ans = 2
octave-2.9.18:3> a = 1
a = 1
octave-2.9.18:4> a = a + 1
a = 2
octave-2.9.18:5> a
a = 2
Miles Osborne CFCS1 4
![Page 5: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/5.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Expressions
More example expressions:
octave-2.9.18:9> a = 10 ; b = 11
b = 11
octave-2.9.18:10> % A comment
octave-2.9.18:10>
octave-2.9.18:10> exp(a)
ans = 2.2026e+04
octave-2.9.18:11> b = (a + 10) * 12;
octave-2.9.18:12>
Miles Osborne CFCS1 5
![Page 6: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/6.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Comparisons
EXP COMPARISON EXP
== equal to∼= not equal to< less than<= less than or equal to> greater than>= greater than or equal to
a > 10
Miles Osborne CFCS1 6
![Page 7: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/7.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Logical Operators
& and| or∼ not
(a == 10) | (b < 0)
Miles Osborne CFCS1 7
![Page 8: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/8.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
IF Statements
if CONDITION
EXP
EXP
end
A CONDITION is a test which evaluates to true or false.
The reservered word end terminates the set of statements.
if (a > b)
disp(’a is greater than b’);
a = 1;
end;
Miles Osborne CFCS1 8
![Page 9: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/9.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
IF Statements
if CONDITION
EXP
else
EXP
end
The reservered word else specifies the expressions that areevaluated if the test is false.
if (a > b)
disp(’a is greater than b’)
else
disp(’b is greater than a’)
end;
Miles Osborne CFCS1 9
![Page 10: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/10.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
IF Statements
if CONDITION
EXP
elseif CONDITION
EXP
else
EXP
end
The reservered word elseif specifies another test that isevaluated if the previous test is false.
Miles Osborne CFCS1 10
![Page 11: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/11.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
IF Statements
if (a > b)
disp(’a is greater than b’)
elseif (a == b)
disp(’b is a’)
else
disp(’b is greater than a)
end;
The elseif statement allows for if statements to be chainedtogether.
Miles Osborne CFCS1 11
![Page 12: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/12.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
FOR-loop
for INDEX = EXP: FINISH
EXP
EXP
end
FOR loops execute a block of code a fixed number of times.
FINISH is a test for when we stop.
FOR loops (and loops in general) can be nested.
Miles Osborne CFCS1 12
![Page 13: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/13.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
FOR-loop
for a = 0: 5
disp(’hello’)
end;
Miles Osborne CFCS1 13
![Page 14: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/14.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
WHILE-loop
while CONDITION
EXP
EXP
end
WHILE loops execute a block of code a variable number oftimes.
A while loop is a generalised for loop.
To break out of a loop mid-way, use the break statement.
Miles Osborne CFCS1 14
![Page 15: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/15.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
WHILE-loop
a = 0;
while a < 10
b = 1;
a = a + 1;
end;
Miles Osborne CFCS1 15
![Page 16: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/16.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Functions
Typically, we want to specify a repeated operation:
A MATLAB function is stored in a file ending with a .m
extension.
The function name must be the same as the file name (lessextension).
MATLAB functions have two parameter lists:
A list of arguments.A list of results.
Arguments can be changed, but that is bad pracice.
Arguments are copied when a function is invoked.
Miles Osborne CFCS1 16
![Page 17: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/17.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Functions
function [output_list] = function_name(input_list)
The first word must be function.
Optional arguments are enclosed in square brackets.
(If there are no arguments, then the brackets are dropped)
Arguments are separated using commas.
function addtwo(x,y)
% add x and y
x + y
Miles Osborne CFCS1 17
![Page 18: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/18.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Functions
function [result] = addtwo(x,y)
% add x and y
result = x + y
Here we have returned the result of adding x and y.
Comments after the function are printed eg:
help addtwo
All variables in a function are local (unless global):
global b;
Miles Osborne CFCS1 18
![Page 19: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/19.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Writing Output
The disp function can write simple messages:
disp(a)
The c-like printf function can write more complex output:
printf(’%d %d\n’,a,b);
Miles Osborne CFCS1 19
![Page 20: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/20.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Reading Input
The input function can read simple input:
b = input(’type a number:’)
The c-like sscanf function can read more complex output:
s = ’2.71 3.14’;
a = sscanf(s,’%f’)
This creates a two-element vector from the stringrepresentation.
Miles Osborne CFCS1 20
![Page 21: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/21.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Files
Files have names: the actual name you see.
Files are manipulated using file handles.
A file handle indicates the position within a file.
Files have modes: append to end, write from scratch etc.
Miles Osborne CFCS1 21
![Page 22: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/22.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Files: Writing
(Example taken fom Web)
output = fopen(’myfile.txt’,’wt’); %’wt’ means write text
if (output < 0)
error(’failed to open myfile.txt’);
end;
a = 10;
fprintf(output, ’A line of text %d\n’,a);
fclose(output);
Miles Osborne CFCS1 22
![Page 23: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/23.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Files: Reading
To read from a file, use fscanf
input = fopen(’myfile.txt’,’rt’); % ’rt’ means read text
if (input < 0)
error(’failed to open myfile.txt’);
end;
a = fscanf(input, ’%d\n’);
disp(a);
fclose(input);
Miles Osborne CFCS1 23
![Page 24: CFCS1 - Matlab Programming · A MATLAB function is stored in a file ending with a .m extension. The function name must be the same as the file name (less extension). MATLAB functions](https://reader030.vdocuments.us/reader030/viewer/2022040909/5e81e6f8a072002f9508be5f/html5/thumbnails/24.jpg)
Data TypesExpressions
Control LoopsFunctions
Input-Output
Summary
MATLAB is a fairly standard programming language.
There is a lot of online help.
MATLAB is quite quirky.
Miles Osborne CFCS1 24