0% found this document useful (0 votes)
40 views8 pages

University of Engineering and Technology, Taxila: Introduction To MATLAB Environment

Matlab is an interactive programming environment for working with matrices and arrays. It allows users to define vectors and matrices, perform operations on them, and view graphical outputs. The Matlab desktop contains a command window, workspace browser, and figure windows. Help is available through the help browser. Vectors and matrices can be defined using brackets and semicolons. Individual elements can be accessed using indices and colons notation.

Uploaded by

Muhham Waseem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views8 pages

University of Engineering and Technology, Taxila: Introduction To MATLAB Environment

Matlab is an interactive programming environment for working with matrices and arrays. It allows users to define vectors and matrices, perform operations on them, and view graphical outputs. The Matlab desktop contains a command window, workspace browser, and figure windows. Help is available through the help browser. Vectors and matrices can be defined using brackets and semicolons. Individual elements can be accessed using indices and colons notation.

Uploaded by

Muhham Waseem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Introduction to MATLAB Environment


MATLAB (matrix laboratory)

Matlab is a commercial "Matrix Laboratory" package which operates as an interactive programming


environment. Matlab program and script files always have filenames ending with ".m"; the
programming language is exceptionally straightforward since almost every data object is assumed to
be an array. Graphical output is available to supplement numerical results.

Online help is available from the Matlab prompt (a double arrow), both generally (listing all available
commands):

>> “Prompt”

The MATLAB Working Environment

The MATLAB Desktop

The MATLAB desktop is the main MATLAB application window. As Fig 1 shows the desktop contains five
sub windows:

1. The Command Window


2. The Workspace Browser
3. The Current Directory Window
4. The Command history Window
5. And one or more Figure 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

The second is a block of explanation called the Help text block

>> help
[a long list of help topics follows]

and for specific commands:

>> help fft


[a help message on the fft function follows. It contains both the H1 line and the help text for fft
function].

How to quit Matlab

The answer to the most popular question concerning any program is this: leave a Matlab session by
typing

quit

or by typing

exit

to the Matlab prompt.

Using the MATLAB editor to create M-Files

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 = [3 1 7 -21 5 6 4];


>>

If you want to view the vector just type its label:

>>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]'

v=

3
1
7
-21
5
6
4

Or you can use the transpose operator ( .’ )


>> w=v.’

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

Similarly to access the second through fourth elements of v we write

>>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(:)

Produces a column vector

>>v(1:end)

Produces a row vector

Indexing is not restricted to contiguous elements

>>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

2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000

Columns 8 through 9

3.7500 4.0000

Accessing elements within a vector

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

Introduction to Matrices in Matlab


A basic introduction to defining and manipulating matrices is given here. It is assumed that you know
the basics on how to define and manipulate vectors using matlab.

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

You can also treat it like a row of column vectors:

>> B = [ [1 2 3]' [2 4 7]' [3 5 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

A 3x3 72 double array


B 3x3 72 double array
v 1x5 40 double array

Grand total is 23 elements using 184 bytes

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= A( : , 3) % this statement picks the third column of the 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 , :)

% this statement picks the 2nd row of the matrix

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

>> A(end , end)

ans =

>> D= A ([1 3] , [2 3])

D=

2 3
7 8

Matrix addressing can also be used in the following way

>> E= logical ([1 0 0; 0 0 1; 0 0 0])

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= sum (A(:))

s=

39

Some important standard arrays are:

For example,

>> A = 5 * ones (3,3)

A=

5 5 5
5 5 5
5 5 5

>> magic (3)

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

You might also like