The purpose of this chapter is to provide an overview of MATLAB syntax and programming, highlighting features that may be underutilized by many users and noting important differences between MATLAB and other programming languages and integrated development environments (IDEs). You should also become familiar with the very detailed documentation that is available from MathWorks in the help browser. The Language Fundamentals section of the MathWorks web site describes entering commands, operators, and data types.
Over the last two decades, MATLAB has matured a lot from its origins as a linear algebra package. Originally, all variables were double-precision matrices. Today, MATLAB provides different variable types, such as integers, data structures, object-oriented programming and classes, and integration with Java. The MATLAB application is a full IDE with an integrated editor, debugger, command history, and code analyzer, and offering report capabilities. Engineers who have worked with MATLAB for many years may find that they are not taking advantage of the full range of capabilities now offered. In this text, we hope to highlight the more useful new features.
The first part of this chapter provides an overview of the most commonly used MATLAB types and constructs. Well then provide some recipes that make use of these constructs to show you some practical applications of modern MATLAB.
MATLAB Language Primer
A Brief Introduction to MATLAB
MATLAB is both an application and a programming language. It was developed primarily for numerical computing and it is widely used in academia and industry. MATLAB was originally developed by a college professor in the 1970s to provide easy access to linear algebra libraries. MathWorks was founded in 1984 to continue the development of the product. The name is derived from MAT rix LAB oratory. Today, MATLAB uses the LAPACK ( L inear A lgebra Pack age) libraries for the underlying matrix manipulations. Many toolboxes are available for different engineering disciplines; this book focuses on features available only in the base MATLAB application.
The MATLAB application is a rich development environment for the MATLAB language. It provides an editor, command terminal, debugger, plotting capabilities, creation of graphical user interfaces, and more recently, the ability to install third-party apps. MATLAB can interface with other languages, including Fortran, C, C++, Java, and Python. A code analyzer and profiler are built in. Extensive online communities provide forums for sharing code and asking questions.
The following are the main components of the MATLAB application:
Command Window The terminal for entering commands and operating on variables in the base workspace. The MATLAB prompt is > > .
Command History A list of previously executed commands.
Workspace display Lists the variables and their values in the current workspace (application memory). Variables remain in memory once created until you explicitly clear them or close MATLAB.
Current Folder The file browser displaying the contents of the current folder and providing file system navigation. Recent versions of MATLAB can also display the SVN status on configuration managed files.
File Details A panel displaying information on the file selected in the Current Folder panel.
Editor The editor for m-files with syntax coloring and a built-in debugger. It can also display any type of text file, and recognizes and appropriately colors other languages, including Java, C/C++, and XML/HTML.
Variables editor A spreadsheet-like graphical editor for variables in the workspace.
GUIDE The graphical interface development window.
Help browser A searchable help documentation on all MATLAB products and third-party products you have installed.
Profiler A tool for timing code as it runs.
These components can be docked in various configurations. The default layout of the main application window or desktop contains the first five components listed and is shown in Figure . The Command Window is in the center. The upper-left panel shows a file browser with the contents of the Current Folder. Under this is a file information display. On the right-hand side are the workspace display and the Command History panel. The base workspace is all the variables currently in application memory. Commands from the history can be double-clicked or dragged onto the command line to be executed. The extensive toolbar includes buttons for running the code analyzer, opening the code profiler, and the Help window, as well as typical file and data operations. Note the PLOTS and APPS tabs above the toolbar. The PLOTS tab allows the graphical creation and management of plots from data selected in the workspace browser. The APPS tab allows you to access and manage the third-party apps that you install.
Figure 1-1.
MATLAB desktop with the Command Window
The Editor with the default syntax coloring is shown in Figure (you can see a file from this chapter). The horizontal lines show the division of the code into cells using a double-percent sign, which can be used for sequential execution of code and for creating sections of text when publishing. The cell titles are bolded in the editor. MATLAB keywords are highlighted in blue, comments in green, and strings in pink. The toolbar includes buttons for commenting code, indenting, and running or debugging the code. The Go To pop-up menu gives access to subfunctions within a large file. Note the PUBLISH and VIEW tabs with additional features on publishing (covered in the next chapter) and options for the editor view.
Figure 1-2.
MATLAB File Editor
The Help browser is shown in Figure . MATLAB offers extensive help, including examples and links to online videos and tutorials. Third-party toolboxes can also install help in this browser. Like any browser, you can open multiple tabs; there is a search utility; and you can mark favorite topics. Topics available in the help browser are referred to throughout this book.
Figure 1-3.
MATLAB Help Window
Everything Is a Matrix
By default, all variables in MATLAB are double-precision matrices. You do not need to declare a type for these variables. Matrices can be multidimensional and are accessed using 1-based indices via parentheses. You can address elements of a matrix using a single index, taken columnwise, or one index per dimension. To create a matrix variable, simply assign a value to it, like this 2 2 matrix a :