Last update(s): Thu Oct 13 15:29:24 2005 ... Thu Mar 23 14:03:04 2017
What is Matlab?
Matlab is a programming language and optional GUI environment for numerical linear algebra. Its computations are normally done in IEEE 754 64-bit arithmetic (Fortran's DOUBLE PRECISION and C/C++/Java's double data types).
Its development started about 1980 at the University of New Mexico in Albuquerque, NM, as an offshoot of the famous EISPACK and LINPACK software library projects. In 1984, Matlab's author (a former Utahn) co-founded a separate commercial company known as The Mathworks, Inc.
The University of Utah has subscribed to a Matlab site license (but only for Unix systems) since about 1990, and consequently, Matlab is widely available on campus, and used in many courses in several different departments.
What is Octave?
Octave is a reimplementation of part of Matlab developed by Professor John Eaton at the University of Texas, Austin, and later at the University of Wisconsin, Madison. It is freely distributed under the GNU General Public License and has its own Web site at http://www.octave.org/. There are pointers there to several topics, including About Octave and a FAQ, plus a discussion of its compatibility with Matlab.
Octave includes a large part of the numerical parts of Matlab, but does not provide a fancy GUI, or support for Matlab toolboxes. Since professional versions of Matlab are quite expensive, some users may find that Octave is an acceptable replacement. Octave provides some limited graphics output with the gnuplot utility.
Octave is a large and complex system, with more than 192,000 lines of C++ and C code, and more than 100,000 lines of Fortran code. We attempt to install each new release of Octave, and most of our systems have it installed, although not always at the latest version. No version has yet successfully built on our GNU/Linux MIPS, PowerPC, and SPARC, OSF/1 Alpha, OpenBSD IA-32, and Apple Mac OS X PowerPC systems.
What books are available for Matlab?
About 450 of them: see the extensive matlab bibliography. Matlab has extremely wide applications in academia and industry, so be careful to look for the books with titles in your specific area of interest.
What documentation is available online for Matlab?
There is a local mirror of the late Kermit Sigmon's widely used Matlab tutorial here.
The vendor has historically provided a large collection of Matlab documents in PDF format. For a complete list, search the FILES files in the Matlab installation tree:
% fgrep -h .pdf /usr/local/sys/matlab/matlab*/FILES ... /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/getstart.pdf /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/graphg.pdf ... /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/programming_tips.pdf /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/refbook.pdf /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/refbook2.pdf /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/refbook3.pdf /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/relnotes.pdf ... /usr/local/sys/matlab/matlab7.0r14/help/pdf_doc/matlab/matfile_format.pdf /usr/local/sys/matlab/matlab7.0r14/help/pdf_doc/matlab/relnotes.pdf /usr/local/sys/matlab/matlab7.0r14sp2/etc/enduser.pdf
For some reason, version 7 comes with many fewer documents than version 6 had. The older manuals are most certainly still usable, but they cannot describe the features added to later versions.
In addition to those PDF files, the other online documentation is the help system inside Matlab. In a Matlab session, type the command help followed by a function name or topic, or use the Help button in the GUI version:
% matlab ... >> help sqrt SQRT Square root. SQRT(X) is the square root of the elements of X. Complex results are produced if X is not positive. ...
What version of Matlab do we have?
We maintain several historical versions of Matlab. You can see what they are like this:
% which matlab /usr/local/bin/matlab % ls /usr/local/bin/matlab* /usr/local/bin/matlab /usr/local/bin/matlab-7.5 /usr/local/bin/matlab-2006b /usr/local/bin/matlab-7.6 /usr/local/bin/matlab-2007b /usr/local/bin/matlab-7.8 /usr/local/bin/matlab-2008a /usr/local/bin/matlab-7.9 /usr/local/bin/matlab-2009a /usr/local/bin/matlab7 /usr/local/bin/matlab-2009b /usr/local/bin/matlab7.0r14 /usr/local/bin/matlab-5.3r11 /usr/local/bin/matlab7.0r14sp2 /usr/local/bin/matlab-6.0r12 /usr/local/bin/matlab7.1r14sp3 /usr/local/bin/matlab-6.0r13 /usr/local/bin/matlabdoc /usr/local/bin/matlab-7.3 /usr/local/bin/matlabdoc-5.3r11 % file /usr/local/bin/matlab /usr/local/bin/matlab: symbolic link to `matlab-7.8'
Thus, at the time of writing, the default was version 8, but releases back to 5.3r11 are available, and the newer version 7.9 (same as 2009b) is also present.
In recent years, two version-numbering systems are used by the vendor, so we alias them like this:
% file /usr/local/bin/matlab-200* /usr/local/bin/matlab-2006b: symbolic link to `matlab-7.3' /usr/local/bin/matlab-2007b: symbolic link to `matlab-7.5' /usr/local/bin/matlab-2008a: symbolic link to `matlab-7.6' /usr/local/bin/matlab-2009a: symbolic link to `matlab-7.8' /usr/local/bin/matlab-2009b: symbolic link to `matlab-7.9'
For stability during academic terms, we normally do not change the default version number except at term breaks.
What Matlab toolboxes do we have?
Use the VER command in a Matlab session. On one local system, we get output similar to this:
% matlab >> VER --------------------------------------------------------------------- MATLAB Version 188.8.131.529 (R2009b) MATLAB License Number: 194152 Operating System: SunOS 5.10 Generic_141414-07 sun4u Java VM Version: Java is not enabled --------------------------------------------------------------------- MATLAB Version 7.9 (R2009b) Simulink Version 7.4 (R2009b) Control System Toolbox Version 8.4 (R2009b) Image Processing Toolbox Version 6.4 (R2009b) MATLAB Compiler Version 4.11 (R2009b) Optimization Toolbox Version 4.3 (R2009b) Signal Processing Toolbox Version 6.12 (R2009b) Simulink Control Design Version 3.0 (R2009b) Symbolic Math Toolbox Version 5.3 (R2009b) System Identification Toolbox Version 7.3.1 (R2009b)
How do I suppress the Matlab GUI?
The Matlab GUI is bloated and slow to start (300MB and 96 shared libraries, versus 99MB and 77 shared libraries for the non-GUI version). If you don't need its features, run Matlab like this:
% matlab -nodisplay -nojvm -nosplash
How do I use high-precision arithmetic?
Standard Matlab carries out computations in IEEE 754 double-precision arithmetic, which provides almost 16 decimal digits of precision, and a number range from about 10-324 to 10308.
% matlab ... >> format long; >> erfc(-7.5) ans = 2 >> sin(1/256) / (1/256) ans = 0.99999745687043 >> factorial(40) ans = 8.159152832478977e+47
On systems where the Symbolic Math Toolbox is available and licensed for use, Matlab can communicate with Maple to perform high-precision computations, at the expense of wrapping expressions inside character strings in function calls. Here is the above example, but now in 50-decimal-digit precision:
% matlab ... >> digits(50) >> vpa('erfc(-7.5)') ans = 1.9999999999999999999999999722335061396943089933603 >> vpa('sin(1/256.0) / (1/256.0)') ans = .99999745687042983799221222193087945750276128903457 >> vpa('40!') ans = 815915283247897734345611269596115894272000000000.
How do I make a simple 2-D function plot?
Matlab does not plot functions, just arrays of points. You therefore have to create suitable arrays first, with enough points in them to produce smooth curves. Here, we create an array of x values with (exact) steps of 1/128 between -5 and +5, and then plot the element-by-element quotient, sin(x)/x:
% matlab ... >> x = [-5 : 1/128 : +5]; >> plot(sin(x)./ x);
Use the menu path File -> Save As to save the plot in a suitable format. The resulting plot looks like this:
The same Matlab plotting commands in Octave result in invocation of the external gnuplot program, and a screen snapshot looks like this:
The gnuplot plot data is stored in a temporary file with a random name, such as /tmp/oct-naSEgw. The temporary file is deleted when octave exits, but before then, with care, you can rescue it by copying into another file, and then use gnuplot directly to produce improved views with better fonts and thicker lines.
How do I make a 3-D surface plot?
As with 2-D plots, Matlab creates surfaces by plotting points, not functions, so you have to create suitable coordinate arrays first:
% matlab ... >> [x,y] = meshgrid([-12 : 1/32 : +12]); >> z = sin(sqrt(x .* x + y .* y)) ./ sqrt(x .* x + y .* y); >> mesh(x,y,z);
The pop-up graphics window looks like this:
The File -> Save As menu path lets you save the plot in more than a dozen formats, including PNG color bitmaps and PDF, suitable for typesetting with pdflatex and pdftex, and Encapsulated PostScript, suitable for typesetting with latex and tex.
However, to capture both the plot and the menus, here we made a screen dump by running the X Window Dump command in a separate terminal window, piping its output into the ImageMagick format-conversion tool:
% xwd | convert - hat.png
xwd waits for you to move the pointer into the desired window and click the mouse to make the snapshot.
Another way to make the screen dump is to convert it to Portable Network Map (PNM) format, and then to PNG format:
% xwd | xwdtopnm | pnmtopng > bad-hat.png
Unfortunately, xwdtopnm inverts the color map, producing this confusing plot:
The PBM/PGM/PNM tools from the PBMPlus package are found in the directory /usr/local/pbmplus which is currently in our default search path. The bug is fixed in the newer NetPBM tools, found in /usr/local/netpbm/bin. This alternative pipeline produces correct output:
% xwd | /usr/local/netpbm/bin/xwdtopnm | \ /usr/local/netpbm/bin/pnmtopng > good-hat.png
With the Matlab figure-window toolbar, you can add a color bar, and change the surface orientation to get an alternate view that looks like this PNG image that was saved via the Matlab menus:
Notice that Matlab did not preserve the gray background that was present in the window snapshots when it saved the PNG file. Whether this is a bug or a feature depends on your subsequent use of the plot.
The 602-page manual, Using Matlab Graphics, in the /usr/local/sys/matlab/matlab6.0r13/help/pdf_doc/matlab/graphg.pdf file cited earlier has much more about Matlab plotting.
The same Matlab plotting commands in Octave result in invocation of the external gnuplot program. The plot takes tens of minutes to complete, and even after reducing the grid step size from 1/32 to 1/8, still takes several minutes. The plot is of very poor quality, and looks like this:
A further reduction in grid step size to 1/2 gives a plot that appears in just a few seconds:
gnuplot provides no facilities for interacting with the plot, so the one view that you see is all that you get. The plot data is stored in a temporary file with a random name, such as /tmp/oct-TgPTTq. With care, before octave exits, you can rescue the data by copying that file into another file, and then use gnuplot directly to produce alternate views.
What is a Matlab FIG file?
The GUI version of Matlab allows you to store a plot in a special binary file called a Matlab FIG file, and that is the default format when you save a plot in the GUI (but not in the text-based version). The FIG format has the advantage that the Matlab GUI's File -> Save as menu path can subsequently be used to save the file in more than a dozen different formats, including the widely used Encapsulated PostScript (EPS) and Portable Network Graphics (PNG) formats.
However, the disadvantage is that the FIG format is known only to Matlab. You cannot edit a FIG file, and you may have to discard it, if it ever becomes corrupted. We have seen cases where such files produced on one Matlab version could not be successfully read on another version that differed only in the minor version number. You should therefore not expect to be able to distribute your FIG files to others. Instead, choose one of the more portable formats that can be handled by a broad range of applications.
Even better, write your Matlab code so that a relatively simple code sequence can be used to generate the plot, even in the absence of the GUI:
% matlab -nodisplay -nojvm -nosplash ... >> x = ...some vector... >> y = ...another vector... >> plot(x,y); >> print('-deps', 'xyplot.eps');
That practice then makes it easy to automate the generation of plots with the Unix make utility.
How can I improve the quality of Matlab graphics?
A problem with most software for producing PostScript illustrations is that line widths are too thin, and text too small, when reduced in size for publication in a typical two-column scientific journal, or as a figure in a book. Lloyd Trefethen recommends in his book Spectral Methods in MATLAB (ISBN 0-89871-465-6, p. xvi) that four Matlab graphics parameters be adjusted. Since these settings are likely to be needed in more than Matlab session, put them in a file in your home directory like this:
% cat $HOME/startup.m set(0, ... 'defaultaxesfontsize', 12, ... 'defaultaxeslinewidth', 0.7, ... 'defaultlinelinewidth', 0.8, ... 'defaultpatchlinewidth', 0.7)
In a Matlab session in an working directory, all that you need to do is issue the command startup to get that file read from your home directory. You can later find the value of one of those parameters like this:
>> get(0, 'defaultpatchlinewidth') ans = 0.7000
I tried to start Matlab, but it refused because of too many concurrent users. What can I do?
Wait a half hour or so, and then retry. If the problem persists, ask systems staff for help. The number of Matlab licenses that we purchase has normally been sufficient.
I'm working on machine X. Why does it not have Matlab?
The Matlab vendor supports only a few of the architectures that we have: Apple Mac OS X on PowerPC, GNU/Linux on AMD64 and IA-32, Microsoft Windows IA-32, OSF/1 Alpha, SGI IRIX MIPS, and Sun Solaris SPARC. The latest version is not available on all of those platforms.
How fast is Matlab on my system?
There is a standard command called bench in Matlab that runs several different computations and shows how the current system performs compared to others for which the vendor has provided measurements. The work it performs changes from release to release, so you cannot use it reliably for comparing different versions of Matlab.
On many modern processors, for energy conservation, the CPU clock speed is lowered when the machine is idle (because energy consumption rises roughly as the cube of the clock rate), so you first need to make it busy. This is most easily done by giving the command a numeric argument: bench(10) runs the test ten times, and shows graphically, and in a numerical table, how this machine performs compared to others.
Is there a local mailing list for questions about Matlab?
No, there is not, but one can easily be created if there is sufficient user demand. In the meantime, students who use Matlab in courses should direct their questions to their instructors.
How do I convert a Matlab .fig figure file to PostScript?
Such files are inscrutable binary representations of Matlab graphical objects, but the conversion is fortunately easy in a Matlab session:
>> h = open('myplot.fig') >> saveas(h, 'myplot.eps', 'eps')
Replacing the last argument with other values known to Matlab produces other graphics file formats, but most are frozen-and-low-resolution bitmap formats that are unsuitable for publication, and introduce unwanted artifacts in the plot if they involve lossy compression, as the JPEG format normally does.