Page 829 - Mechatronics with Experiments
P. 829
®
®
MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION 815
The “otherwise” part is executed only if none of the preceding case expressions match the
switch expression. Only the statements between the matching “case” and the next “case,”
“otherwise,” or “end” are executed. Unlike C, the “switch” statement does not fall through,
so “break” statements are not necessary.
®
The various operators used in MATLAB are summarized below.
Arithmetic operators,
A+B
A-B
A∗B A.∗B
A/B A./B
A\B A.\B
AˆB A.ˆB
A’ A.’
Relational operators,
A < B
A > B
A <= B
A >= B
A == B (equal ?)
A˜= B (not equal ?)
Logical operators,
A & B ; AND element-by-element (bit-wise) evaluation
A | B ; OR element-by-element (bit-wise) evaluation
A && B ; AND logical
A || B ; OR logical
˜A ; NOT
xor(A,B) ; Exclusive OR,
logical 1 (TRUE) where either A or B, but not
both, is non-zero.
®
A.1.3 Functions in MATLAB : M-script files and
M-function files
®
There are two different ways to call functions in MATLAB :
1. run another M-file: script file,
®
2. call another MATLAB function in an M-file: function file.
To run another M-file, called a script file, just include the name of that file in the program
M-file. It is equivalent to including the content of that M-file at that location. The data
®
in the M-file is global and accessible in the MATLAB workspace as well as the current
workspace. One M-script file can include and execute another M-script file, and in turn that
file can include yet another M-script file. The nesting can continue as part of the program
logic.
Script M-file example: Matrix1.m
% Filename: Matrix1.m
% Example of an M-script file.
% Typing the file name in MATLAB® command window or another
MATLAB® M-file is