Lec 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MatLab programming Dr.

Tariq Hussein

MatLab Programming

Syllabus:
- Introduction
- Arithmetic operations with scalars
- Display formats
- Built-in functions
- Script files
- Creating one and two dimensional arrays
- Array addressing (Vector, Matrix)
- Script files for managing data
- Two dimensional plots
- Relational and logical operators

References:
- MatLab: An introduction with applications, 5th Edition, Amos Gilat, 2015.

- Programming in MatLab: A problem solving approach, 1 st Edition, Patel and

Mittal, 2014.

- MatLab programming for numerical analysis, 1st Edition, Cesar Lopez,

2014.
MatLab programming Dr. Tariq Hussein

Introduction

MATLAB is a powerful language for technical computing. The name MATLAB


stands for MATrix LABoratory, because its basic data element is a matrix (array).
MATLAB can be used for math computations, modelling and simulations, data
analysis and processing, visualization and graphics, and algorithm development.

MATLAB is widely used in universities and colleges in introductory and advanced


courses in mathematics, science, and especially engineering.

To start MATLAB, double-click the icon named MATLAB on your personal


computer’s desktop.
The MATLAB working environment window will pop up as shown in Figure
below:

This is an interactive environment where you can start entering commands and
obtain results instantly.
MatLab programming Dr. Tariq Hussein

The figure above shows a shortcut for command desktop.

Four windows appear on the MatLab default view:


- Command Window,
- Figure Window,
- Editor Window,
- Help Window.

Command Window: The Command Window is MATLAB 's main window and
opens when MATLAB is started. It is convenient to have the Command Window as
the only visible window. This can be done either by closing all the other windows,
or by selecting Command Window Only in the menu that opens when the Layout
MatLab programming Dr. Tariq Hussein

icon on the Toolstrip is selected. To close a window, click on the pulldown menu at
the top right-hand side of the window and then select Close.

Figure Window: The Figure Window opens automatically when graphics


commands are executed, and contains graphs created by these commands. An
example of a Figure Window is shown in the figure below.

Editor Window: The Editor Window is used for writing and editing programs. This
window is opened by clicking on the New Script icon in the Toolstrip, or by clicking
on the New icon and then selecting Script from the menu that opens. An example of
an Editor Window is shown in the figure below.
MatLab programming Dr. Tariq Hussein

Help Window: The Help Window contains help information. This window can be
opened from the Help icon in the Toolstrip of the Command Window or the toolbar
of any MATLAB window. The Help Window is interactive and can be used to obtain
information on any feature of MATLAB. The figure below shows an open Help
Window.
MatLab programming Dr. Tariq Hussein

Working in the command window


Notes for working in the Command Window:
• To type a command, the cursor must be placed next to the command prompt (>>).
• Once a command is typed and the Enter key is pressed, the command is executed.
However, only the last command is executed. Everything executed previously (that
might be still displayed) is unchanged.
MatLab programming Dr. Tariq Hussein

• Several commands can be typed in the same line. This is done by typing a comma
between the commands. When the Enter key is pressed, the commands are executed
in the order from left to right.
• It is not possible to go back to a previous line that is displayed in the Command
Window, make a correction, and then re-execute the command.
• A previously typed command can be recalled to the command prompt with the up
arrow key (↑). When the command is displayed at the command prompt, it can be
modified if needed and then executed. The down-arrow key (↓) can be used to move
down the list of previously typed commands.
• If a command is too long to fit in one line, it can be continued to the next line by
typing three periods ... (called an ellipsis) and pressing the Enter key. The
continuation of the command is then typed in the new line. The command can
continue line after line up to a total of 4,096 characters.

The semicolon (;):


When a command is typed in the Command Window and the Enter key is pressed,
the command is executed. Any output that the command generates is displayed in
the Command Window. If a semicolon (;) is typed at the end of a command, the
output of the command is not displayed. Typing a semicolon is useful when the result
is obvious or known, or when the output is very large. If several commands are typed
in the same line, the output from any of the commands will not be displayed if a
semicolon instead of a comma is typed between the commands.
Typing %:

When the symbol % (percent) is typed at the beginning of a line, the line is
designated as a comment. This means that when the Enter key is pressed the line is
not executed. This has no effect on the execution of the command. Comments are
frequently used in a program to add descriptions or to explain the program.
The clc command:
The clc command (type clc and press Enter) clears the Command Window. After
typing in the Command Window for a while, the display may become very long.
Once the clc command is executed, a clear window is displayed. The command does
MatLab programming Dr. Tariq Hussein

not change anything that was done before. For example, if some variables were
defined previously, they still exist and can be used. The up-arrow key can also be
used to recall commands that were typed before.
The Command History Window:
The Command History Window lists the commands that have been entered in the
Command Window. This includes commands from previous sessions. A command
in the Command History Window can be used again in the Command Window. By
double-clicking on the command, the command is re-entered in the Command
Window and executed. It is also possible to drag the command to the Command
Window, make changes if needed, and then execute it. The list in the Command
History Window can be cleared by selecting the lines to be deleted and then right-
clicking the mouse and selecting Delete Selection. The whole history can be deleted
by right-clicking the mouse and selecting choose Clear Command History in the
menu that opens

Arithmetic operations with scalars


Numbers can be used in arithmetic calculations directly (as with a calculator) or
they can be assigned to variables, which can subsequently be used in calculations.
The symbols of arithmetic operations are:

You might also like