Академический Документы
Профессиональный Документы
Культура Документы
Contents
M-Files.
Scripts Vs M-Functions
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
Matlab
19/7/2011
Intro to Programming
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:
Matlab
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
19/7/2011
Intro to Programming
OOP Programming
Four
main concepts
Matlab
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
19/7/2011
Flow Control
Rational Operation
==:equal <
: less than > : greater than <= : less than or equal >= : greater than or equal ~= : not equal
Matlab
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
Flow Control
Conditional Statements
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
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
Flow Control
The general form of a WHILE statement is: WHILE expression statements END
Ex:
x=1
while x <= 10
x = 3*x end
Matlab
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
19/7/2011
M-Files.
are global Can be called by writing its name in the command window
Matlab
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
19/7/2011
M-Files.
Function
[mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = sum(x) / n; stdev = sqrt(sum((x - mean).^2)/n);
Matlab
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);
Matlab
19/7/2011
Multidimensional Array
It
Temperature
Matlab
19/7/2011
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
19/7/2011
Structures
Multidimensional
:- s=struct(name,mohamed,score,83,grade,A+);
Matlab
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
19/7/2011
THANK YOU!
Prepared by: Mohamed Farag Email: Mody.eng2008@gmail.com