MATLAB? is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran.
MATLAB also features a family of application-specific solutions called toolboxes. Toolboxes are collections of MATLAB functions (M-files) that extend the MATLAB environment in order to solve particular classes of problems.
To find out what toolboxes are available on a system, type the command ver on the matlab prompt.
Where is MATLAB?
MATLAB is available in the DECS Public Labs, Remote Desktop Services, and the DECS Compute Servers. Matlab may be acquired for any computer through the MSU Computer store, or can be installed on Engineering owned machines by DECS.
Invoking MATLAB on the DECS Compute Servers
To invoke MATLAB on the DECS Compute Servers enter the command matlab in a terminal window. If you have connected via X2Go you will be presented with the full Matlab GUI. If you have simply made an ssh connection, the MATLAB banner appears with the MATLAB prompt ">>". At this point, the MATLAB interpreter is awaiting instructions from you. To enter a small matrix, place brackets around the data and separate the rows with semicolons:
A = [ 1 2 3; 4 5 6; 7 8 9]
When you press Return, MATLAB responds with:
A =
1 2 3
4 5 6
7 8 10
To invert this matrix, enter
B = inv(A)
Editing the Command Line
To edit mistyped commands or recall previous command lines, you can use the arrow keys. For example, if you misspell sqrt in the command
log(sqtr(atan(2*(3+4))))
Instead of retyping the entire line, press the "up" key. The last command you entered displays. You can press key to move the cursor left, backspace to delete r and the r key to insert the r in the right place.
The commands you enter during a MATLAB session are stored in a moderately sized input buffer. You can "smart recall" to recall a previous command whose first few characters you specify.
Using Editors
As you become proficient with MATLAB, you will do an increasing amount of work with M-files. M-files contain a series of MATLAB statements and these files are created and edited with text editors such as vi, Emacs, etc. You can use a separate window for running the editor. M-file is a record of how a problem was solved and automates command entry as well as allows user defined function that can be executed from the command line in the future.
Shell Escape
Use the exclamation point character "!" within MATLAB to indicate the input line is a command to the operating system. This is useful for running any UNIX utility or other program, without exiting from MATLAB. When the external process completes, control returns to MATLAB.
Help Facility
A help facility provides on-line information for most MATLAB topics. The command help with no arguments displays a list of directories that contain MATLAB related files. Each line displayed lists the name of a directory followed by a description of the directory contents. Some of the directories are associated with the base MATLAB itself; they contain information about the functions that are built in the core MATLAB processor, or included M-files with every copy of MATLAB. Other directories are for toolboxes; these contain collections of additional MATLAB functions covering more specialized application areas. For a list of functions covered by a particular directory, type help followed by the directory name (help matfun). Often the names listed are functions, so help followed by one of these names provides detailed information about that feature (help eig).
A more general information search is provided by lookfor. This command is like using the index in a book and is similar to the UNIX apropos command (lookfor inverse). Three other commands, who, what and which give information about variables, file and directories. Type help who, help what and help which to find out details.
For additional help, you may try Matlab's site on the Internet at http://www.mathworks.com.
Demos
Typing demo at the MATLAB prompt demonstrates some of MATLAB's capabilities and uses a menu called Expo.
Data Input
- Enter data as an explicit list of elements. If you have a small amount of data, say 10-15 elements, it is easy to type the data explicitly using brackets, "[" and "]". This method is awkward for larger amounts of data because you can't edit your input if you make a mistake.
- Create data in an M-file. Use your text editor to create a script M-file that enters your data as an explicit list of elements. This method is useful when your data is not already in a computer readable form and you have to type them in anyway. Running and re-running the M-file enters the data.
- Load data from an ASCII flat file. A flat file stores the data in ASCII form, with fixed length rows terminated with newlines (carriage returns), and spaces separating the numbers. ASCII flat file can be directly edited by normal text editors. Flat files can be read directly into MATLAB using the load command. The result is put in a variable whose name is the file name.
- Read data using fopen, fread and MATLAB's other low level I/O functions. This method is useful for loading data file from other applications that have their own establish file formats.
Exporting MATLAB Data
- Save the data in ASCII form using the save command with the -ascii option.
- Write the data in a special format using fopen, fread and MATLAB's other low level I/O functions. This is useful for writing data files in a format required by other applications.
Recording a MATLAB Session
The diary command creates a diary of your MATLAB session in a disk file. (Graphics are not saved) The resulting ASCII file is suitable for inclusion into reports and other documents using any word processor.
Printing Graphics
MATLAB's various plotting functions create windows that contain graphics. The print filename command can be executed to create a postscript file of the active figure which is placed in your root directory. From a unix prompt you can then send the postscript file to the printer using the "lp" command.
How To Start MATLAB in Background
Before attempting to run a matlab program in background, remove any graphic commands, such as plot, etc from your file, my_file.m. Then use the following to run your m-file in background on one of the servers:
nohup matlab < my_file > outfile &
The 'nohup' will enable the program to keep running after you log off the server. The '&' will tell the server to run the command in background. Outfile is where your output will be stored.
If you are running in the background, you will probably also want to save the output from your variables also.
save foo [var1 [var2 [...]]]
This will create a file called foo.mat. If you do not specify any variables, then Matlab will save your entire workspace (this can be quite large). To retrieve the data in foo, use 'load foo' in the Matlab window.
Things To Try
( type as seen at the MATLAB prompt >>)
sqrt(-1) x = [-1 0 2] x = x' y = x - 1 inprod = x'*y outprod = x*y' who A = [1 2 3; 4 5 6; 7 8 0] B = A' B(1,1) B(:,1) B(1:2,:) x(3) C = A + B b = A*x x = inv(A)*b z = pi*x who w = x*y w = x.*y u = x^2 u = x.^2
z = cos(pi/3) z = [-1:0.05:1]' zz = cos(2*pi*z) plot(z,zz) plot(z,zz,z,zz,'ro') zz = exp(-1.5*z).*cos(2*pi*z) plot(z,zz,z,zz,'ro') title(`Figure 1'); xlabel(`Time'); ylabel(`Amplitude'); grid
(create a file called bumps.m in your root directory. It should include:) function y = bumps(x) y = 1./((x-.3).^2+.01) + 1./((x-.9).^2+.04) - 6 (then at the prompt >>) x = -1:0.01:2; plot(x,bumps(x)) fplot(`bumps',[-1 2],0.01)
x=-8:0.5:8; y = x; [X,Y]=meshgrid(x,y) plot(X,Y,'.') R = sqrt(X.^2 + Y.^2) +eps Z = sin(R)./R contour(x,y,Z,10) mesh(x,y,Z) meshc(x,y,Z)
MATLAB Command and Function Tables
The 20 categories listed below provide command and function reference tables for their associated topic. To find "general" commands and functions for MATLAB, at the MATLAB prompt ">>", enter:
help general
To find MATLAB operators and special characters, at the MATLAB prompt ">>", enter:
help ops
Category Description -------- ------------------------------------------------ general General purpose commands ops Operators and special characters lang Language constructs and debugging elmat Elementary matricies and matrix manipulation specmat Specialized matricies elfun Elementary math functions specfun Specialized math functions matfun Matrix functions - numerical linear algebra datafun Data analysis and Fourier transform functions polyfun Polynomial and interpolation functions funfun Function functions - nonlinear numerical methods sparfun Sparse matrix functions plotxy Two dimensional graphics plotxyz Three dimensional functions graphics General purpose graphics functions color Color control and lighting model functions sounds Sound processing functions strfun Character string functions iofun Low-level file I/O functions demos Demonstrations and samples