MATLAB Basic Tutorial


Jump to: navigation, search
MATLAB Basic Tutorial
Green carrot left.gif
All MATLAB Articles
Green carrot.jpg
In order to prevent spam, users must register before they can edit or create articles.

1 Introduction

MATLAB is dynamically compiled. Instead of compiling all the necessary code, generating an executable, and then running executable MATLAB interprets the code. This doesn't mean that the code isn't compiled at all but rather the compiling is done in stages attempting to balance spped against versatility and command line functionality.

It is possible to do calculations one line at a time at the command line using the same syntax as would be used in an m-file. This functionality is useful for testing specific lines of code but isn't efficient for routines of more than dozen lines or so. At around the dozen command lines it make more sense to write those lines into a script or function. Both allow for code reuse but a script runs in the base workspace while function has its own workspace.

MATLAB is a good tool for testing algorithms, creating models, and general exploration. It is not ideal for running heavy duty, repetitive tasks. MATLAB can do these tasks but the balance between functionality and speed that Mathworks has chosen means that heavy duty tasks take longer to complete than the equivalent code in compiled form. As a result many of the base functions of MATLAB are compiled by Mathworks. These are stable, mature functions that are core to MATLAB's varsatility. As a result of these functions being compiled C executables every thing runs faster.

2 Basic Command Line Functions

The best way to learn simple MATLAB commands are by diving right in. Here are your basic operations:

  • Addition
 >> 3 + 2
        ans =
  • Subtraction
 >> 3 - 2
        ans =
  • Multiplication
 >> 3 * 2
        ans =
  • Division
 >> 3 / 2
        ans =

If you wish to assign the answer to a variable for later use then the = operator is required.

 >> a = 3 + 2
        a =

To supress the output use a semicolon at the end of the statement.

 >> a = 3 + 2;
  • Supressing the output is useful when running functions, loops, are creating variables to store large vectors or arrays.

The simplest means of creating a large array is use on of the special matrices. For example

 >> b = zeros(1000, 1E4);

creates a matrix b with 1000 rows and 10,000 columns. Notice that the use of scientific notation - 1E4.

3 Calling Functions

As with any programming language reusable code in the form of functions is at the heart of good efficient programming. The reuse of code leads to faster development of new tools and more confidence in the end result. Functions are written in m-files. The first line of a function m-file must be

 function [ouput1, output2, ...] = function_name(input1, input2, ...)

Calling a function from the command line is the same whether the function was written by Mathworks or the guy in the next cubical.

 [ouput1, output2, ...] = function_name(input1, input2, ...);

Help files for MATLAB functions can be obtained using the help command

 help function_name

It's a good exercise to attempt that now. Try help std and see what is displayed at the command line. The menu comes with options such as File, Edit, ..., Help. Under the help heading is an option - Product Help. This brings up the online, web browser based help, and if std is typed into the Search For: box then it will display help for the standard deviation function named std.

When a function is called MATLAB first looks in the current directory (which can be seen at the top or by typing pwd at the command line). If the function is not present in the current directory then MATLAB searches the path for a function by that name. The path is a set of directories where MATLAB will search for functions and data files. The user can decide which directories MATLAB searches and in what order MATLAB searches them.

4 Scripts vs. Functions

Scripts and functions perform much the same purpose in MATLAB. Both are m-files used to store code for reuse. The main difference between a script and function is that a script works in the calling workspace (typically this is the base workspace). A function works in its own workspace.

The base workspace is the workspace where all the variables created at the command line are stored. A function creates its own workspace which has only the variable provided to it by the calling workspace. The calling workspace can be another function or the base workspace.

Scripts operate in the calling workspace which means that scripts can use and modify variables stored in the calling workspace but cannot be passed variables to use and modify. Somtimes operating on variables in the calling workspace is desirable, possibly even necessary, but most of the time this is sloppy programming.

Functions operate in a new workspace with only the variables provided to it by the calling workspace in the function call. Sometimes the list of variables required for a given function can be long and the use of a script becomes desirable. Typically it is better to save the necessary variables to a file, then pass the file name with these variables to the desired function. This keeps the workspace clean and prevents problems such as calling a script with a loop where the loop variable is i from a loop in the calling function where the loop variable is also i. The use of functions also prevents the accidental overwrite of a variable that is needed downstream.

4.1 Editing

The primary tool for creating and editting m-files is the MATLAB editor. It is convenient and free with MATLAB. However, it can use a lot of system resources on Windows machines. Also, during tasks which take a long time - such as reading in a large data file or executing a very large loop - the MATLAB editor can become unavailable due to the resources MATLAB is already consuming. There are several free editors available that come with most of the nice features present in the MATLAB editor. Since they are separate programs Windows will allow their use during heavy system use by MATLAB due to long execution time tasks. This author recommends Crimson Editor. Gabe 01:48, 13 April 2008 (CDT)