2. Salient Features of MATLAB
MATLAB is a
powerful language
for technical
computing
The name MATLAB
stands for MATrix
LABoratory, because
its basic data
element is a matrix
(array).
It integrates
computation,
visualization, and
programming in an
easy-to-use
environment i.e.
Integrated
development
environment (IDE).
It has a very
extensive library of
predefined programs
or functions
designed to help
engineers and
scientists to solve
their problems in a
faster and less
painful way
3. There is no need for
memory management
It is platform-independent
A new program can be
developed easily using the
predefined functions
There is extensive graphics
support
Advantages of
Matlab
5. SUBWINDOWS IN MATLAB DESKTOP
COMMAND WINDOW
CURRENT FOLDER
COMMAND HISTORY
WORKSPACE
MATLAB
WINDOWS
6. SNAPSHOT OF MATLAB DESKTOP
Command History window,
which displays all entries
made in the command window
during each session.
This window shows files and
directory available in the current
working directory of MATLAB
It is the window where we type commands, execute
programs, and launch other windows
This lists variables that you have either
entered or computed in your MATLAB
session
7. Matlab Basic numeric
Data types
Unsigned integer
(4 different classes)
Uint 8 Uint 16 Uint 32 Uint 64
Signed integer
(4 different classes)
Int 8 Int 16 Int 32 Int 64
12. Abort In order to abort a command in MATLAB, hold down the
control key and press c to generate a local abort with MATLAB.
The Semicolon (;) If a semicolon (;) is typed at the end of a
command, the output of the command is not displayed.
Typing % When per cent symbol (%) is typed in the
beginning of a line, the line is designated as a comment.
When the enter key is pressed, the line is not executed.
13. SCRIPT FILES
A script file is a sequence
of MATLAB commands,
also called a program.
When a script file runs
(is executed), MATLAB
executes the commands
in the order.
When a script file has a
command that generates
an output, the output is
displayed in the
Command Window.
Using a script file is
convenient because it can
be edited and executed
many times.
Script files can be typed
and edited in any text
editor and then pasted
into the MATLAB editor.
Script files are also called
M-files because the
extension .m is used
when they are saved.
17. • In some cases variable is defined in the script file, and when the file
is executed, the user is prompted to assign a value to the variable in
the Command Window.
• This is done by using the input command for creating the variable.
• The form of input command that defines the characters that are
entered as a string
The input command
18.
19. The disp command
• The disp command is used to display the
elements of a variable without displaying the
name of the variable, and to display text.
• Every time the disp command is executed, the
display it generates appears in a new line.
• The format of disp command is
20.
21. The fprintf command can be used to display output
(text and data) on the screen.
With this command (unlike with the disp command)
the output can be formatted. For example, text and
numerical values of variables can be intermixed and
displayed in the same line.
The fprintf command
26. In for-end loops the execution of a
command, or a group of commands, is
repeated a predetermined number of times
Each round of execution is called a pass.
Structure of a for-end loop
27. while-end loops are
used in situations when
looping is needed but
the number of passes is
not known in advance.
looping process carry
on until a specified
condition is satisfied.
Structure of while –end
loop
28. Example of for- end
loop
Result of example
Example of while - end
loop
Result of example
29. NESTED for LOOPS
A for loop can be nested within another for loop.
for k = 1 : 3
for n = 1 : 5
.
commands
.
end
end
Every time k is increased by 1 the
nested loop loops five times with
the value of n ranging from 1
through 5.
k = 1 n = 1 k = 2 n = 1 k = 3 n = 1
k = 1 n = 2 k = 2 n = 2 k = 3 n = 2
k = 1 n = 3 k = 2 n = 3 k = 3 n = 3
k = 1 n = 4 k = 2 n = 4 k = 3 n = 4
k = 1 n = 5 k = 2 n = 5 k = 3 n = 5
Overall the commands will be
executed 15 times with the values
of:
30. matrix = 0;
n = input('Enter the number of rows ');
m = input('Enter the number of columns ');
for i = 1:n
for j = 1:m
if i == j
matrix(i,j) = 1;
else
matrix(i,j) = 7;
end
end
end
disp('The matrix is:')
disp(matrix)
Nested
loop
>> Example2
Enter the number of rows 4
Enter the number of
columns 2
The matrix is:
1 7
7 1
7 7
7 7
31.
32.
33.
34. Breaking out of loops 1
Example:
A = 6 B = 15
count = 1
while A > 0 & B < 10
A = A + 1
B = B + 2
count = count + 1
if count > 100
break
end
end
• Break out of the loop after 100 repetitions if the while
condition has not been met
39. Syntax errors
These errors mainly occur as a result of the
misspelling of variable or function names or from
missing quotes or parentheses.
40. Runtime errors are found by Matlab
during the execution of a program.
They are generally more difficult to fix
than simple syntax errors.
The capability to fix runtime errors is
something that improves with
experience only.
Run time
Errors
48. To direct Matlab to run the command in line 1, click on the Step icon
Matlab runs the first line and then proceeds to line 2.
49. An alternative way to step through the program is by pressing the F10 key on the keyboard. Press
F10 to make Matlab run line 2 of the code.
50. Move the mouse and place the cursor directly over the variable x. A yellow box pops up and tells you the
values of x.
51. Press F10 to run line 3. Using the mouse, highlight the expression (x(k) > 0). Place the mouse cursor over
the highlighted area and right-click. Choose Copy from the Context Menu.
52. Press F10. The code steps to line 7. It should have entered the if statement and gone to line 5 to
change the sign of the first element, but it did not. Instead it went to line 7. By noting this, we can
discover that there is a programming error in the condition for the if statement. The greater than
character “>” should be changed to the less than character “<”.
53. Fix this bug by changing the character “>” to “<”. Then go to the Menu – Debug - Exit Debug Mode.
This terminates the debugger in Matlab.
54. The bug is now fixed. Remove the Breakpoint. This can be carried out by using the mouse. Left-click in
the space between the code and the line number 1 (pointed at by the arrow in the figure) to toggle the
Breakpoint to off. The red dot disappears.
55. Profiler
Let a Matlab program consists of a M-file that
may call a number of different Matlab
functions.
To find out the time that is required to execute
the script file and all the functions that it calls
,Matlab contains a tool that provides us with
this information ,called the Profiler.
Use this information to optimize our code and
make it run faster.