This page is maintained by the software tutors. For errors and/or amendments please contact the current tutor supporting the program.
MATLAB (Matrix Laboratory) is software designed mainly for numerical computing. As opposed to other programming languages, such as Fortran or C/C++, MATLAB is designed not to demand low level programming from the user (e.g. matrix inversion is done with one simple command). In particular, it is very easy to use MATLAB for matrix manipulations, optimisation problems and visualising the results.
Due to its simplicity, MATLAB is generally slower than the aforementioned programs. However, with MATLAB versions 6.5 and later, speed is significantly increased and in late versions Matlab is often as fast as Fortran. It should be pointed out that Matlab could be particularly slow in performing loops. You can use matrix manipulations to speed up your code by vectorising for instance, for loops.
Please check here for details.
How to Get the Software
MATLAB is installed on all PCs running the Research Configuration (tipically Computer Rooms and computers assigned to Fellows).
The current version of the software and of its modules is detailed here.
If you are running on a Staff Configuration (tipically Office PCs) and want to use the software, after having checked if you are eligible, please contact the EUI Helpdesk.
A fully licensed copy of MATLAB and all its modules is available to eligible EUI members: for further details click here.
MATLAB distinguishes between two types of "m-files" (all Matlab files end with ".m", thus "m-file"): Scripts and Functions. A script is a document that includes a collection of commands. When executing a script MATLAB simply reads the commands line by line and executes them in a descending order.
Functions are similar to scripts, but they make use of their own local variables and accept input arguments. An m-file specifying a function always includes the function definition lines:
function [output] = functionname(input)
in the beginning of the document. The file name of a separate function, less its extension should correspond to the declared function name.
MATLAB program files can contain code for more than one function. Additional functions within the file are called subfunctions. This is an example of subfunctions:
function [output1] = functionname1(input1)
function [output2] = functionname2(input2)
Open Matlab. Click "File --> New file --> m-file". Let's start by writing a script.
At the beginning of the document, type:
The command "clear" clears out any previous data stored on the workspace, and "clf" clears any previous graphs.
Following these lines, type:
n = 3;
A = rand(n,n); % An n-by-n matrix of rand numbers
B = eye(n,n); % An n-by-n identity matrix
C = A*B; % Matrix multiplication
D = C-A % Element by element subtraction
and save the file as "myfirstmfile.m" somewhere on your computer. Now go to the "current directory" on the top of your screen, and choose the folder where you saved your file. Type "myfirstmfile" in MATLAB command window. The value of D (which should be zero) should show on your screen.
The example shows that you don’t need to declare the type of variables used in the program, but assign the values to the variables straight away. Notice that a semi-colon after a command suppresses output to the screen, and a percentage symbol (%) allows for commenting the part of the code that Matlab will ignore while executing the file. Remember that Matlab is case-sensitive.
EXERCISE: Write the saved script as a function which takes "n" input and returns "D" as output.
Here you can find the Matlab presentation made by software assistants (01.02.2011).
Some Matlab tutorials that may be useful:
As a last piece of advice, MATLAB's help function is excellent and should be used extensively.
Page last updated on 17 August 2017