MATLAB is an interactive program used for numerical calculations and visualization. It includes several commands and functions, and you can also make your own.
To better understand MATLAB, you can look at its name - which is a contraction for MATrix LABoratory. All calculations are based upon matrices and linear algebra.
The program is widely used in technology-studies at NTNU.
It is possible to widen MATLAB's use with 'toolboxes' to enable easier solving of problems in special topics. As an example, SIMULINK is a tool for simulating dynamical non-linear systems. NTNU provides the following toolboxes for employees connected to a license server:
For students, these are the available toolboxes:
As long as you are connected to the net, you can as a student or employee install and run MATLAB on your own machine. It is available from Progdist and Orakeltjenesten: Software at NTNU. The license is retrieved from a server and allows the usage of the products listed above.
On a Windows machine or Macintosh, you will get a 'Matlab Command Window' when starting MATLAB. All calculations are done by commands in MATLAB, and graphics are displayed in a dedicated graphics window when needed, which means that you will have to 'command' MATLAB to open it first.
When starting MATLAB in a UNIX-environment, you type matlab in a terminal. The calculations are run in that terminal and graphics are displayed in another windows by X-Windows.
After MATLAB has started, you'll see a some introduction-text and >> where you can enter commands.
To exit MATLAB, you can either type exit or close the window.
MATLAB has its own introduction slideshow and by typing the command intro it will run through some examples and you can view the .m-file to see which commands that have been used. A more in-depth demonstration can be shown by typing demo.
To get help using MATLAB, you can both use the dropdown-help in the menubar, or different commands in the 'Matlab Command Window':
>>help acos ACOS Inverse cosine ACOS(X) is the arccosine of the elements of X. Complex results are obtained if ABS(X) > 1.0 for some element. Overloaded methods help sym/acos.m
By choosing "Full Documentation Set" on the bottom of the page we get when typing helpdesk, we'll get access to all manuals. They are shown in PDF by Acrobat Reader (UNIX: acroread).
In addition to this, you can get the manuals for MATLAB on MathWorks' homepage: Documentation for MathWorks Product Family
Get previous line: arrow-up, Ctrl-P (Unix, Windows, Mac)
Get next line: arrow-down, Ctrl-N (Unix, Windows, Mac)
Move one character to the left: arrow-left, Ctrl-B (Unix, Windows, Mac)
Move one character to the right: arrow-right, Ctrl-F (Unix, Windows, Mac)
Move left and delete a character: Delete (Unix), Backspace (Windows, Mac)
Move a word to the left: Ctrl-L (Unix), Ctrl-arrow-left (windows)
Move a word to the right: Ctrl-R (UNIX), Ctrl-arrow-right (Windows)
Move to the beginning of a line: Ctrl-A (Unix, Windows, Mac)
Move to the end of a line: Ctrl-E (Unix, Windows, Mac)
Abort existing line: Ctrl-U (Unix, Mac), Esc (Windows, Mac)
Delete a character where your marker is:Ctrl-D (Unix, Windows, Mac), Del (Windows, Mac)
We will try to give some examples of how to use MATLAB here, but for further use, search the web for your problem or ask a teacher.
Adding a constant to all elements in a matrix:
>> a = [1 2 3 4 5]
>> b = a + 10
b = [11 12 13 14 15]
To display a vector graphically:
>> plot(b)
>> bar (b)
The first command will give a line plots the vector as a line and the second as a bar chart.
Calculations involving matrices:
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
Transpose the matrix A and name it B: >> B=A'
B=
1 4 7
2 5 8
3 6 9
Multiplying two matrices:
>> A * B
Multiplying each matrices elementwise:
>> A .* B
Inverse matrix:
>> inv(A)
Instead of interactive use of MATLAB, it is possible to make functions; a sequence of operations in an .m-file. Say there is something you want to do to several objects which uses several commands, it is then easier to write a function for it and use that instead of repeating the commands.
By using a normal text editor you can make your own .m-files. Comments to explain the commands or what you do start with %. The first commentline in the function-file is what is shown when you use lookfor. The following comments in a function before an open line is shown by typing help name_of_function.
To use a function, just type the name of it:
>> name_of_file
We can choose where we want to save our functions, but if we want to use them we have to tell MATLAB where it can find it. As a standard on Windows, MATLAB looks for functions in the /work-part of the MATLAB-directory. You can change this if you want to keep your functions in another workspace.
As mentioned earlier, you can add different toolboxes to MATALB to extend its functionality (a toolbox is a collection of functions).
To get information about a toolbox and its functions, type help name_of_toolbox:
simulink/simulink - SIMULINK toolbox/control - Control System Toolbox. images/images - Image Processing Toolbox. nnet/nnet - Neural Network Toolbox. nnet/nndemos - Neural Network Demonstrations and Applications. toolbox/optim - Optimization Toolbox. toolbox/pde - Partial Differential Equation Toolbox. toolbox/signal - Signal Processing Toolbox. toolbox/stats - Statistics Toolbox.
To see demonstrations of how you can use MATLAB and its toolboxes, see help and the topics matlab/demos, simulink/simdemos, images/imdemos and nnet/nndemos in addition to help name_of_toolbox.
MathWorks encourages distribution of useful home-made functions. You can find these on an anonymous catalog on their ftp server ftp.mathworks.com(144.212.100.11).
You can access these catalogs on a normal web page as well: The MATLAB Central file exchange.
MathWorks also distributes a newsletter here: The MathWorks MATLAB Digest (http://www.mathworks.com/company/newsletters/).
For printing graphical plots you can use the command print.
You can also save a graphical plot in different formats, by using the command print -djpeg name_of_file. Or you can use File-Export where you will be given a choice of different file formats such as JPEG, TIFF, PNG and EPS.
It is possible to print the calculation part as well, by diary filename.txt. Everything you then type in the "Matlab Command Window" will then be written to that file. You can turn this on and off by diary on and diary off.
If you haven't done that, and still want copy of the calculations, you can in Windows/Macintosh use File-Print/Print Selection. For the UNIX version you have to copy the text, paste it into a text editor and then print.
It is possible to use both C and Fortran in MATLAB. These functions are called MEX-files. For more information you can see the MATLAB manual: External Interface Guide.
There are several ways to import data to MATLAB:
MATLAB's I/O functions are based on C.
Export: There are several ways to export data from MATLAB: