Matlab
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:
- Signal processing
- Optimization
- Statistics
- Control Systems
- Image Processing
- Matlab Compiler
- PDE Toolbox
- Neural Networks
For students, these are the available toolboxes:
- Signal processing
- Optimization
- Control Systems
- Image Processing
- SimPowerSystem
- PDE Toolbox
Information about NTNU's license for MATLAB
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.
Opening and closing MATLAB
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.
Demonstration of MATLAB
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.
Getting help
To get help using MATLAB, you can both use the dropdown-help in the menubar, or different commands in the 'Matlab Command Window':
-
help
Help using this command is displayed in the 'Matlab Command Window' by first displaying a list of topics you can get help on or you can write help command_you_want_help_on. As an example, by writing help acos, you'll get this:konsoll #1>>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
-
helpwin
Almost the same as help, but is displayed in a new window and is easier to navigate in.
-
helpdesk
Comprehensive hypertext documentation using a web browser. Takes some time to start and requires that you installed the documentation when installing MATLAB.
-
lookfor
By typing lookfor with a topic, MATLAB will search both original and home-made functions. To learn more about lookfor use the command help lookfor. -
ver
Gives information on what version you're using of MATLAB and toolboxes.
-
whatsnew
By typing whatsnew you'll get information about the program from Readme.txt.
Manuals
- MATLAB - for all systems:
- Getting Started With Matlab
- Using Matlab
- Using Matlab Graphics
- Application Program Interface Guide
- Building GUIs with Matlab
- Matlab Notebook User's Guide.
- Using SIMULINK
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
How to edit in a commandline
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)
Examples
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)
Functions
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.
Toolbox
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.
Demonstration of toolboxes
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.
Distributing your functions
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.
C and Fortran in MATLAB
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.
Export and import
There are several ways to import data to MATLAB:
- Define a variable containing the data
- Use the editor and define the data in the same way in an .m-file as a script.
- Import data from a 'flat ASCII file'. A 'flat' file stores all data in ASCII format with a certain length for the rows, spaces between numbers and line feed after each line. You import these with the command load filename.
- Using MATLAB's In/Out-functions like fopen and fread. Often used when retrieving data from other programs.
- Create a MEX-file for reading data - used if there already exists C or Fortran functions for reading data from other programs.
MATLAB's I/O functions are based on C.
Export: There are several ways to export data from MATLAB:
- As mentioned above, you can use the command diary and later edit the file the command calls.
- It is possible to save data in ASCII formatData kan lagres i ASCII format by using save filename.txt name_of_variable -ascii.
- Cut and paste from MATLAB to a text editor.
- Use In/Out functions such as fwrite.
- Create a MEX-file. This and the former method is often used when exporting data to another program.
- Print data as a MAT-file with the command load and use a C or Fortran program to convert this data to whichever format you want.
Miscellaneous:
- MATLAB supports four commands for files and directories on your machine: dir(show list of files), type(display the contents of a file), delete(delete file), cd (change directory).
- You can make a command continue onto the next line when ending the current one with .... If we end a command with semicolon, the result will not be shown. It is possible to type severeal MATLAB commands on the same line with commas between.
- A variable or function name has to start with a letter and can contain up to 19 characters.
For more information, see these pages (also mentioned in the article):
| Språk: | Kommentarer: | 0 | Aid: | 537 | Brukervurdering: |
|
|
| Sist oppdatert: | 2007-07-31 | Forfatter: | livermor | Lest: | 402 ganger |