Вы находитесь на странице: 1из 25

MATLAB CECE ACADEMIC TEAM

Prepared by: Mohamed Farag

Contents

Intro to Programming Flow Control


Conditional Statements. Loops.

M-Files.

Scripts Vs M-Functions

New Data Types


Multidimensional Arrays Structures Cell Arrays

Error Handling
CECE Academic Team 19/7/2011

Matlab

Intro to Programming

Design of computer programs to solve complex problems needs to be done in a systematic manner from the start to avoid time-consuming and frustrating difficulties later in the process. Programming Types
Algorithms

and Control Structures Structured Programming OOP Programming

Matlab

CECE Academic Team

19/7/2011

Intro to Programming

Algorithms and Control Structures

an ordered sequence of precisely defined instructions that performs some task in a finite amount of time There are three main categories of algorithmic operations:

Sequential Operation Conditional Operation Iterative Operation

Matlab

CECE Academic Team

19/7/2011

Intro to Programming

Structured Programming

A technique for designing programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure. In MATLAB these modules can be built-inor user-defined functions.

Matlab

CECE Academic Team

19/7/2011

Intro to Programming

OOP Programming
Four

main concepts

Encapsulation Data Hiding Inheritance Polymorphism

Matlab

CECE Academic Team

19/7/2011

Flow Control

Logical Operation
~

: not & : And | : Or && : Short circuit And ( Scalar operation) || : Short Circuit Or ( Scalar Operation)

Note:- ~:not operation is an element-wise operation(it affects each element in the array )

Matlab

CECE Academic Team

19/7/2011

Flow Control

Rational Operation
==:equal <

: less than > : greater than <= : less than or equal >= : greater than or equal ~= : not equal

Matlab

CECE Academic Team

19/7/2011

Flow Control

Masking
If we want to do a specific operation on specific elements under certain conditions. We have two ways:

Using loops and loop over the array and check the condition on each element then do your operation Using Masks

Ex:
A=[1 2 3;-1 -2 -3;4 5 6]; A(A<=0)=0; A= 12 3; 0 0 0 ; 4 5 6

Matlab CECE Academic Team 19/7/2011

Flow Control

Conditional Statements

IF Conditionally execute statements.

The general form of the IF statement is

IF expression statements ELSEIF expression statements ELSE statements END Example if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end Matlab CECE Academic Team 19/7/2011

Flow Control

Conditional Statements

SWITCH Switch among several cases based on expression. The general form of the SWITCH statement is: SWITCH switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement OTHERWISE, statement, ..., statement END
Matlab CECE Academic Team 19/7/2011

Flow Control
Example

t = [0 : 100]; x = exp(-t) . *sin(t);


response = input( 'Type min, max , or sum .',' s ' ) response = lower ( response ) ; switch response case min

minimum = min (xl maximum = max (x) total = sum(x)

Case max

case sum

otherwise disp ( ' You have not entered a proper choice . ) End
Matlab CECE Academic Team 19/7/2011

Flow Control

Loops

For Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END

Example

for R = 1:N for C = 1:N A(R,C) = 1/(R+C-1); end end


Matlab CECE Academic Team 19/7/2011

Flow Control

WHILE Repeat statements an indefinite number of times.

The general form of a WHILE statement is: WHILE expression statements END

Ex:

x=1

while x <= 10
x = 3*x end

Matlab

CECE Academic Team

19/7/2011

Flow Control

The break statement. A while loop can be terminated with the break statement, which passes control to the first statement after the corresponding end. The break statement can also be used to exit a for loop. The continue statement can also be used to exit a for loop to pass immediately to the next iteration of the loop, skipping the remaining statements in the loop.

Matlab

CECE Academic Team

19/7/2011

M-Files.

M-Files can be used to write Scripts & Functions


Functions
need

input & output arguments Variables are local


Scripts
Variables

are global Can be called by writing its name in the command window

Matlab

CECE Academic Team

19/7/2011

M-Files.

Functions: Style
Function

[Outputs]=function_name(inputs) % discription of the Function it will be displayed when you use help function_name Function body

Matlab

CECE Academic Team

19/7/2011

M-Files.

Functions Types: Primary


Ex: function

Function

[mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n);

Matlab

CECE Academic Team

19/7/2011

M-Files.

Functions Types:

Subfunctions:

Function defined in the same primary function file and used inside it. it is not visible outside the file. Ex:

function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = avg(x,n); stdev = sqrt(sum((x-avg(x,n)).^2)/n);

%------------------------function mean = avg(x,n) %AVG subfunction mean = sum(x)/n

Matlab

CECE Academic Team

19/7/2011

New Data Types

Multidimensional Array
It

is an array but with more than two subscripts Ex:- A=ones(3,5,2);


Used

for 3D data like:

Temperature

in a room A sequence of Matrix

Matlab

CECE Academic Team

19/7/2011

New Data Types

Cell Array
A

cell array is a collection of containers called cells in which you can store different types of dataSome functions
Cell:create

empty cell array Celldisp:display cell array Cellplot:display grahpical representation of cell array

Matlab

CECE Academic Team

19/7/2011

New Data Types

Structures
Multidimensional

arrays accessed by textual field

designators Ex: S.name=mohamed;


S.score=83; S.grade=A+; Or

:- s=struct(name,mohamed,score,83,grade,A+);

Matlab

CECE Academic Team

19/7/2011

Error Handling

Try- Catch

Ex:

function matrixMultiply(A, B) try A*B catch err = lasterror; if(strfind(err.message, 'Inner matrix dimensions')) disp('** Wrong dimensions for matrix multiply') Else if(strfind(err.message, 'not defined for values of class')) disp('** Both arguments must be double matrices') end end end
CECE Academic Team 19/7/2011

Matlab

References
[1] Using Matlab version 6 , Mathworks [2] Introduction to Matlab 7 for engineers, William J [2] David F. Griffiths , An Introduction to Matlab. [3] Matlab Help.

Matlab

CECE Academic Team

19/7/2011

THANK YOU!
Prepared by: Mohamed Farag Email: Mody.eng2008@gmail.com

Вам также может понравиться