The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to provide easy access to matrix software developed by the LINPACK (linear system package) and EISPACK (Eigen system package) projects. MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming environment.
Furthermore, MATLAB is a modern programming language environment: it has flexible data structures, contains built-in editing and debugging tools, and supports object-oriented programming. These factors make MATLAB an excellent tool for teaching and research filed oriented. MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for solving technical problems.
The software package has been commercially available since 1984 and is now considered as a standard tool at most universities and industries worldwide. It has powerful built-in routines that enable a very wide variety of computations. It is also has easy to use graphics commands that make the visualization of results immediately available.
Specific applications are collected in packages referred to a toolbox. There are toolboxes for signal processing, symbolic computation, control theory, simulation, optimization, and several other fields of applied science and engineering using MATLAB.
A Minimum MATLAB Session:
The goal of this minimum session (also called starting and exiting sessions) is to learn the first steps:
- MATLAB Interface
- MATLAB Basics
- Programming In MATLAB
- How to quit MATLAB
After logging into your account, you can enter MATLAB by double-clicking on the MATLAB short cut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB, a special window called the MATLAB desktop appears. The desktop is a window that contains other windows.
The major tools within or accessible from the desktop are:
- The Command Window
- The Command History
- The Workspace
- The Current Directory
When MATLAB is started for the first time, the screen looks like the one that shown in the Figure 1.1. This illustration also shows the default configuration of the MATLAB desktop. You can customize the arrangement of tools and documents to suit your needs. Now, we are interested in doing some simple calculations. We will assume that you have sufficient understanding of your computer under which MATLAB is being run. You are now faced with the MATLAB desktop on your computer, which contains the prompt (>>) in the Command Window. Usually, there are 2 types of prompt: >> for full version EDU> for educational version.
Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign, though our MATLAB version is for educational purpose.
- COMMAND WINDOW Users are allowed to type MATLAB codes or basic unix commands into the command window. Press <return> to execute the command. Input command lines follow after a \>>” symbol, while printed results typically do not. For instance,
As another example, type pwd (the unix command \print working directory”) to see current folder. Type clc command to clear commands displayed in the window. Features such as <tab>-completion and <up>-key for command history are available in the command window
- COMMAND HISTORY shows previous commands executed in the command window. Other history features are available at the command window (<up> key, drag-n-drop)
- CURRENT FOLDER shows the content of the current working directory
- WORKSPACE shows the variables. After you type a = 1+1 in the command window, you see appear in the workspace
- EDITOR MATLAB BUILT-IN EDITOR, which can be called out by clicking “New Script” or pressing <ctrl>+N (or <cmd>+N). (You can use your own editor!)
- The Basic Features:
Let us start with something simple, like defining a row vector with components the numbers 1, 2, 3, 4, 5 and assigning it a variable name, say x.
» x = [1 2 3 4 5]
1 2 3 4 5
To create a column vector (MATLAB distinguishes between row and column vectors, as it should) we can either use semicolons ( ; ) to separate the entries, or first define a row vector and take its transpose to obtain a column vector. Let us demonstrate this by defining a column vector y with entries 6, 7, 8, 9, 10 using both techniques.
» y = [6;7;8;9;10]
» y = [6,7,8,9,10]
6 7 8 9 10
Let us make a few comments. First, note that to take the transpose of a vector (or a matrix for that matter) we use the single quote ( ‘ ). Also note that MATLAB repeats (after it processes) what we typed in. Sometimes, however, we might not wish to “see” the output of a specific command. We can suppress the output by using a semicolon ( ; ) at the end of the command line. Finally, keep in mind that MATLAB automatically assigns the variable name ans to anything that has not been assigned a name. In the example above, this means that a new variable has been created with the column vector entries as its value. The variable ans, however, gets recycled and every time we type in a command without assigning a variable, ans gets that value.
It is good practice to keep track of what variables are defined and occupy our workspace. Due to the fact that this can be cumbersome, MATLAB can do it for us. The command whos gives all sorts of information on what variables are active.
Name Size Elements Bytes Density Complex
ans 5 by 1 5 40 Full No
x 1 by 5 5 40 Full No
y 1 by 5 5 40 Full No
Grand total is 15 elements using 120 bytes
A similar command, called who, only provides the names of the variables that are active.
Your variables are:
ans x y
Vectors and matrices:
We have already seen how to define a vector and assign a variable name to it. Often it is useful to define vectors (and matrices) that contain equally spaced entries. This can be done by specifying the first entry, an increment, and the last entry. MATLAB will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 1, 2, 3,…, 7, 8, you can type
» u = [0:8]
0 1 2 3 4 5 6 7 8
Here we specified the first entry 0 and the last entry 8, separated by a colon ( : ). MATLAB automatically filled-in the (omitted) entries using the (default) increment 1. You could also specify an increment as is done in the next example.
To obtain a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:
» v = [0:2:8]
0 2 4 6 8
Here we specified the first entry 0, the increment value 2, and the last entry 8. The two colons ( : ) “tell” MATLAB to fill in the (omitted) entries using the specified increment value.
MATLAB will allow you to look at specific parts of the vector. If you want, for example, to only look at the first 3 entries in the vector v, you can use the same notation you used to create the vector:
0 2 4
The following command lists the first 4 entries of the vector v, using the increment value 2:
Defining a matrix is similar to defining a vector. To define a matrix A, you can treat it like a column of row vectors. That is, you enter each row of the matrix as a row vector (remember to separate the entries either by commas or spaces) and you separate the rows by semicolons ( ; ).
» A = [ 1 2 3; 3 4 5; 6 7 8 ]
1 2 3
3 4 5
6 7 8
We can avoid separating each row with a semicolon if we use a carriage return instead. In other words, we could have defined A as follows
» A = [
1 2 3
3 4 5
6 7 8]
1 2 3
3 4 5
6 7 8
which is perhaps closer to the way we would have defined A by hand using the linear algebra notation.
PROGRAMMING IN MATLAB:
M-files: Scripts and functions:
To take advantage of MATLAB’s full capabilities, we need to know how to construct long (and sometimes complex) sequences of statements. This can be done by writing the commands in a file and calling it from within MATLAB. Such files are called “m-files” because they must have the filename extension “.m”. This extension is required in order for these files to be interpreted by MATLAB.
There are two types of m-files: script filesand function files. Script files contain a sequence of usual MATLAB commands, that are executed (in order) once the script is called within MATLAB. For example, if such a file has the name compute.m , then typing the command compute at the MATLAB prompt will cause the statements in that file to be executed. Script files can be very useful when entering data into a matrix.
Function files, on the other hand, play the role of user defined commands that often have input and output. You can create your own commands for specific problems this way, which will have m the same status as other MATLAB commands. Let us give a simple example. The text below is saved in a file called log3.m and it is used to calculate the base 3 logarithm of a positive
number. The text file can be created in a variety of ways, for example using the built-in MATLAB editor through the command edit (that is available with MATLAB 5.0 and above), or your favorite (external) text editor (e.g. Notepador Wordpadin Microsoft Windows). You must make sure that the filename has the extension “.m” !
function [a] = log3(x)
% [a] = log3(x) – Calculates the base 3 logarithm of x.
a = log(abs(x))./log(3);
% End of function
To end your MATLAB session, type quit in the Command Window, or select File —> Exit MATLAB in the desktop main menu.