University of Engineering and Technology, Taxila: Introduction To MATLAB Environment
University of Engineering and Technology, Taxila: Introduction To MATLAB Environment
Online help is available from the Matlab prompt (a double arrow), both generally (listing all available
commands):
>> “Prompt”
The MATLAB desktop is the main MATLAB application window. As Fig 1 shows the desktop contains five
sub windows:
The Command Window is where the user types MATLAB Commands and expressions at the prompt
>>and where the output of those commands are displayed
MATLAB defines the workspace as the set of variables that the user creates in a work session. The
Workspace Browser shows these variables and some information about them. Double Clicking on a
variable in the workspace browser launches the Array Editor, which can be used to obtain information
and in some instances edit certain properties of the variable
The Current Directory tab above the workspace tab shows the contents of the current directory, whose
path is shown in the Current Directory Window
For example in the windows operating system the path might be as follows:
C:\MATLAB\Work, indicating that directory “Work” is a subdirectory of the main directory
“MATLAB”,which is installed in drive C. Clicking on the arrow in the Current Directory Window shows a
list of recently used paths. Clicking on the button to the right of the window allows the user to change
the current directory
The Command history Window contains a record of the commands a user has entered in the command
window, including both current and previous MATLAB sessions. Previously entered MATLAB commands
can be selected and re-executed from the command history window by right clicking on a command or
sequence of commands. This action launches a menu from which to select various options in addition to
executing the commands
Getting Help
The Principle way to get help is to use the MATLAB Help Browser, opened as a separate window either
by clicking on the question mark symbol (?)on the desktop toolbar, or by typing helpbrowser at the
prompt in the Command Window
>>helpbrowser
[MATLAB help browser will be opened where you can get information about various topics]
The Help Browser consist of two panes, the help navigator pane, used to find information, and the
display pane, used to view the information
Search field also exists on the navigator pane where you can type either specific words, function
names or any phrase to get information which you are looking for:
Another way to get help for a specific function is by typing doc followed by the function name at the
command prompt
[ e.g typing doc format followed displays documentation for the function called format in the display
pane of the Help Browser]
M- Functions have two types of information that can be displayed by the user.
The first is called the H1 line, which contains the function name and the one line description
>> help
[a long list of help topics follows]
The answer to the most popular question concerning any program is this: leave a Matlab session by
typing
quit
or by typing
exit
The MATLAB editor is both a text editor specialized for creating M-files and a graphical
MATLAB debugger
The editor can appear in the window by itself, or it can be a sub window in the desktop. M-
files are denoted by the extension .m as in introlab.m.
The MATLAB editor window has numerous pull down menus for tasks such as saving, viewing
and debugging files. Because it performs some simple checks and also uses colors to
differentiate between various elements of code, this text editor is recommended as the tool
of choice for writing and editing M-Functions
To open the editor type edit at the prompt in the command window.
Introduction to Vectors in Matlab
This is the basic introduction to Matlab. Creation of vectors is included with a few basic operations.
Topics include the following:
1. Defining a vector
2. Accessing elements within a vector
Defining a Vector
Matlab is a software package that makes it easier for you to enter matrices and vectors, and
manipulate them. The interface follows a language that is designed to look a lot like the notation use in
linear algebra.
Almost all of Matlab's basic commands revolve around the use of vectors. A vector is defined by placing
a sequence of numbers within square braces:
>> v = [3 1 7 -21 5 6 4]
v=
3 1 7 -21 5 6 4
This creates a row vector which has the label "v". The first entry in the vector is a 3 and the second
entry is a 1. Note that matlab printed out a copy of the vector after you hit the enter key. If you do not
want to print out the result put a semi-colon at the end of the line:
>>v
v=
3 1 7 -21 5 6 4
Notice, though, that this always creates a row vector. If you want to create a column vector you need
to take the transpose of a row vector. The transpose is defined using an apostrophe ("'"):
v=
3
1
7
-21
5
6
4
w=
3
1
7
-21
5
6
4
To access block of elements, we use MATLAB’s colons notation, e.g to access the first three elements of
v we write
>>v(1:3)
ans=
3 1 7
>>v(2:4)
ans=
1 7 -21
Similarly to access all elements from some specific location say the 3 rd through the last element
>>v(3:end)
ans=
7 -21 5 6 4
>>v(:)
>>v(1:end)
>>v(1:2:end)
ans=
3 7 5 4
>>v(end:-2:1)
ans=
4 5 7 3
A common task is to create a large vector with numbers that fit a repetitive pattern. Matlab can define
a set of numbers with a common increment using colons. For example, to define a vector whose first
entry is 1, the second entry is 2, the third is three, up to 8 you enter the following:
>> v = [1:8]
v=
1 2 3 4 5 6 7 8
If you wish to use an increment other than one that you have to define the start number, the value of
the increment, and the last number. For example, to define a vector that starts with 2 and ends in 4
with steps of .25 you enter the following:
>> v = [2:.25:4]
v=
Columns 1 through 7
Columns 8 through 9
3.7500 4.0000
You can view individual entries in this vector. For example to view the first entry just type in the
following:
>>v(1)
ans =
This command prints out entry 1 in the vector. Also notice that a new variable called ans has been
created. Any time you perform an action that does not include an assignment matlab will put the label
ans on the result.
A vector can be used as an index into another vector. For example we can pick the first, third sixth and
seventh elements of v using the command
>>v([1 3 6 7])
v=
3 7 6 4
Defining Matrices
Defining a matrix is similar to defining a vector. To define a matrix, you can treat it like a column of
row vectors (note that the spaces are required!):
>> A = [ 1 2 3; 3 4 5; 6 7 8]
A=
1 2 3
3 4 5
6 7 8
B=
1 2 3
2 4 5
3 7 8
If you have been putting in variables through this and the tutorial on vectors, then you probably have
a lot of variables defined. If you lose track of what variables you have defined, the whos command will
let you know all of the variables you have in your work space.
>>whos
Name Size Bytes Class
You can work with different parts of a matrix, just as you can with vectors. Again, you have to be
careful to make sure that the operation is legal.
To extract the element in the 2nd row and 3rd column we write,
>>A(2,3)
ans =
5
The colon operator is used in the matrix indexing to select a two dimensional block of elements out of
matrix:
C=
3
5
8
The colon operator can also be used in the matrix indexing to extract the row of the matrix
>> C= A (2 , :)
C=
2 4 5
>>A(1:2,3:4)
??? Index exceeds matrix dimensions.
>>A(1:2,2:3)
ans =
2 3
4 5
>>A(1:2,2:3)'
ans =
2 4
3 5
To create a matrix B equal to A but with its last column set to 0’s we write
>> B= A;
>> B (: , 3)=0
B=
1 2 0
2 4 0
3 7 0
ans =
D=
2 3
7 8
E=
1 0 0
0 0 1
0 0 0
Now if we write
>>A(E)
ans =
1
5
The use of a colon is useful in case to find the sum of all elements of a matrix
s=
39
For example,
A=
5 5 5
5 5 5
5 5 5
ans =
8 1 6
3 5 7
4 9 2
Finally, sometimes you would like to clear all of your data and start over. You do this with the "clear"
command. Be careful though, it does not ask you for a second opinion and its results are final.
>>clear
LAB TASK:
Defining a matrix A = [ 1 8 3;5 2 0 ; 4 1 9]
1. extract the element in the 2nd row and 3rd column
2. picks the 3rd row of the matrix
3. picks the 2nd column of the matrix
4. create a matrix B equal to A but with its last column set to 0’s
5. find the sum of all elements of a matrix