Getting Started With Matlab

From Eigenvector Research Documentation Wiki
Revision as of 08:45, 4 September 2008 by imported>Scott
Jump to navigation Jump to search

About MATLAB

MATLAB, an acronym for MATrix LABoratory, is a product of The MathWorks, Inc., (Natick, MA). MATLAB is built around the LAPACK and BLAS linear algebra engines. It is a programming/computational environment especially suited to problems in matrix algebra, allowing the user to do matrix computations in a computing language that looks very much like standard linear algebra notation. MATLAB, which is required to run PLS_Toolbox, is not an inexpensive package (as you probably already know). However, because of its large collection of built-in functions, powerful graphics, easy-to-learn language, extensibility, and flexible environment, it is a great value. Another positive aspect of MATLAB is that it is available for many platforms, including Mac OS X, Microsoft Windows, Unix, and Linux. Thus, PLS_Toolbox can be run on all these platforms.

MATLAB can be used in the "command line" mode, where single line commands are entered at the command prompt (>>), executed immediately, and the results displayed. MATLAB contains hundreds of commands; a complete description of MATLAB commands is contained in the manuals provided with the software and through onscreen help.

We have continued to expand our use of GUIs (Graphical User Interfaces) with PLS_Toolbox and Solo. All of our major chemometric routines are available via GUIs and include a sophisticated layout allowing quick access to tools and information, including contextual menus, short cuts, and integrated help.

Before entering the PLS_Toolbox GUI environment the user must first enter and navigate the command mode. Thus, some minimal understanding of the command mode is required even of the beginner. Although a good number of powerful methods are available through our GUIs, there are still several routines only accessible from the command line. It is the intention of this section to get the new MATLAB user sufficiently comfortable with the command line mode to load data, do simple manipulations of data, execute PLS_Toolbox GUI routines, and finally save data and models to permanent media.

Startup

After starting MATLAB from the Desktop, the default three-part window will appear as shown in Figure 2-1. Clicking the Start button in the lower left-hand corner will display a list of MATLAB tools, shortcuts, and documentation. Mousing over the top item, MATLAB, will display a submenu that should have PLS_Toolbox listed at or near the bottom. Should it not be found, please install PLS_Toolbox as described in Chapter 1, Installing PLS_Toolbox.

Figure 2.1. Default MATLAB desktop at startup. Command Window The heart of MATLAB is the Command Window. It is where commands can be directly entered by typing them followed by a return. The >> prompt shows that the Command Window is ready for entering a command. Together, MATLAB and PLS_Toolbox contain hundreds of commands in the form of scripts and functions. A script is simply a series of commands that are executed when the command is called. A function is similar to a script except that arguments are passed to it when it is called and it returns other arguments calculated by the function. A script runs in the base Workspace and does not return value. Once a script or function has been defined, it can be called from the command window or by other functions or scripts. One of the most useful commands for a beginner is demo. (Note that MATLAB is case sensitive and the commands Demo, DEMO and demO would be interpreted by MATLAB as different (and most likely undefined) commands.) If the user types demo in the Command Window (followed by a return), the MATLAB help browser will appear at the Getting Started with Demos page. It is well worth the time for a new user to explore both MATLAB and PLS_Toolbox demonstrations. Another useful command is the help command. It is executed by typing help followed by the name of the command. For example, type help demo to see instructions on using the demo command. The demo command requires that you remember the exact name of the command. If you cannot remember the command name, then use the lookfor command followed by a key word. For example, lookfor demonstration will cause MATLAB to search for all commands that use the word demonstration in the first line of their help file.

Figure 2-2. MATLAB demos window. Finally, provided the Documentation files have been loaded into the computer when MATLAB was installed, detailed help may be obtained using the Help menu choice in the top MATLAB command bar. A record of executed commands can be viewed by scrolling up the Command Window or by examining the Command History window. Entering Data Before MATLAB can do calculations, it needs data to work on. Typing the following command:

» A = [2 5 3 6
7 3 2 1
5 2 0 3]

defines the variable A equal to the matrix enclosed within the square brackets. The spaces between the numbers delineate the columns and each return starts a new row. The same results are obtained if we use the command:

» A = [2 5 3 6; 7 3 2 1; 5 2 0 3]

where the returns have been replaced by semicolons. Either way MATLAB will respond by typing the resulting matrix:

A =

   2   5   3   6
   7   3   2   1
   5   2   0   3

If you do not wish to see MATLAB echo (print out) the result, end the command with a semicolon. For example, if you type:

» A = [2 5 3 6; 7 3 2 1; 5 2 0 3];

the variable A will be defined as before, but MATLAB will not echo the result. When working with very large matrices, the use of the semicolon can prevent thousands of elements from being printed on the screen.