0% found this document useful (0 votes)
22 views80 pages

Commonly Used Bloks - Chap5

Modelling Skill in Simulink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views80 pages

Commonly Used Bloks - Chap5

Modelling Skill in Simulink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 80

Dingyu Xue, YangQuan Chen

System Simulation Techniques with


MATLAB and Simulink
A JOHN WILEY & SONS, INC., PUBLICATION 2013

CHAPTER 2
FUNDAMENTALS
OF MATLAB PROGRAMMING
Dingyu Xue
Northeastern University, Shenyang, China

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Chapter2 Funamentals
of MATLAB Programming
The main content
 MATLAB Environment
 Data Types in MATLAB
 Matrix Computations in MATLAB
 Flow Structures
 Programming and Tactics of MATLAB Functions
 Two-dimensional Graphics in MATLAB
 Three-dimensional Graphics
 Graphical User Interface Design in MATLAB
 Accelerating MATLAB Functions
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.1 MATLAB Environment
The current version of MATLAB is R2013b (or
MATLAB version 8.0)
 Released by MathWorks Inc. in September 2013
 Two versions are released each year, in March and
September respectively and labeled versions a and b.
The graphical interface of MATLAB
 CommandWindow, Current Folder window,
CommandHistory window and Workspace window;
Single CommandWindow is allowed
MATLAB Online Help and Documentation
 Help | MATLAB Help
 http://www.mathworks.com PDF files
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.2 Data Types in MATLAB

MATLAB has many powerful and accurate


numerical facilities.
Double-precision floating point data type is used as
the default data type in MATLAB.
 Composed of 8 bytes(64 bits), following the IEEE standard
 With 11 exponential bits, one sign bit and 52 bits
 The value range is
 Its MATLAB description is double()

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Other data types
 int8() ,int16() ,int32() ,uint16() and uint32()
 symbolic data type, strings, multidimensional
arrays, structured arrays, cells, classes and objects

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.2.1 Constants and Variables
Constants (reserved strings)
 eps: Machine-specific error tolerance for the floating point
operations. Default value
 I and j: Imaginary unit; The square root of -1, i=sqrt(-1)
 Inf: Infinity, -Inf represents negative infinity
 NaN: Obtained by 0/0, Inf/Inf, (Not a Number)
 Pi: A double-precision representation of the circumference
ratio 
Variables:
 Led by a letter, followed by other letters, digits and unders
cores.
 Case sensitive
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.2.2 Statements
Two types of statements:
 Direct assignment:
Structure:
If put a semicolon at the end of the statement, the variable will
not be displayed
If the left hand side variable name isn’t given, the result will
be returned to the reserved variable ans
 Function call statement:
Structure:

Correspond to *.m file


The same function may be called by different syntaxes
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.2.3 Matrix Representation
The complex double-precision matrix is the basic
MATLAB variable type.
The matrix

can be entered with the following command

The matrix extension

Without the definition of CHAPTER


dimension in advance
2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Colon expression

E.g.
The complex matrices

The following statements can be used

Converting a matrix into symbolic form


CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.2.3 Multidimensional Arrays
Three-dimensional arrays

MATLAB commands

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.3 Matrix Computations in MATLAB

The main content


Algebraic Computation
Logical Operations
Comparisons and Relationships
Data Type Conversion

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.3.1 Algebraic Computation
Matrix transpose: A’, A.’
Addition, subtraction, multiplication and divisions:
A+B, A-B, A*B, A\B and B/A
Flipping and rotation: flipud(A), fliplr(A), rot90(A)
Power of a matrix: A^x
Dot operation: A.*B, A.^2, A.^A
Kronecker product: kron(A, B)

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Examples of Algebraic Computation

E.g.1: solve for the cubic roots of


The first cube root

The other two can be obtained by rotating

E.g.2: statements of product and dot product

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.3.2 Logical Operations
The description of logical variables
 Logical variables: logical()
 Double-precision expression: nonzero value is logical “1”
Logical Operations
 logical "and": A&B
 logical "or": A | B
 logical "not": ~A
 logical "exclusive or": xor(A,B)
Logical operations are the operations of the correspon
ding elements
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.3.3 Comparisons and Relationships

Comparisons and Relationships


 Relationships >, <, >=, <=, = =, ~=…
 find(), any(), all()
E.g.

Get the indexes

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.3.4 Data Type Conversion
Integral Functions
 floor(A): finding the next integer towards negative infinity
 ceil(A): finding the next integer towards positive infinity
 round(A): finding the nearest integer
 fix(A): finding the integer next integer towards 0
Other Functions
 [n,m]=rat(A): rational approximation
 mod(A,k), rem(A,k): modulus and remainder
 gcd(n,m), lcm(n,m): the greatest common divisor and least
common multiple
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.4 Flow Structures
As a programming language, MATLAB
supports different flow structures.
Similar to C, more flexible than C
The main content
 Loop Structures
 Conditional Structures
 Switches
 Trial Structure

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.4.1 Loop Structures
For structures and while structures

Solve for s1=>i

i between End loop


s1 and s2? No
If true? End loop
Yes No
Loop statements Yes
Loop statements
i+s3=>i

while structure
for structure
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
EXAMPLES

E.g.1: Solve for

Two structures, the latter is more complicated


 for structure
 while structure

E.g.2: Find the minimum m:

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.4.2 Conditional Structures

No
If true?
Yes
Conditional statements

Yes No
Condition

Conditional Conditional
statements 1 statements 2

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
No
Condition 1
No
Condition 2
Yes
Yes No
Condition n

Yes

Statements 1 Statements 2 Statements n Statements n+1

Consider again m:

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.4.3 Switch Structure
Switches

 Switches end after statements executed


 If expression k is the same as one of the expressions k2, · · · ,
km, where they are composed of a cell data type (bounded by
{ and })
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 If none of the expressions are met, the statements
under the otherwise keyword will be executed.

Switch Structure

Equal Equal Equal Equal Equal


Expression 2
expression 3
Expression 1 Otherwise
… ... ...
expression m

Statements 1 Statement 2 Statement n

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.4.4 Trial Structure
Trial Structure

 The statements 1 are executed first, if an error occurs, the


statements 2 are executed.
The trial structure is very useful in programming
practice.
 We can put a very fast algorithm in the try statements
 And put a slow but reliable algorithm in the catch statement
s to guarantee a solution.
 The solution to certain problems may be made more effecti
ve.
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.5 Programming and Tactics of
MATLAB Functions
M-script file
Why we need MATLAB functions ?
 The main design mode in MATLAB
 Encapsulated variables, encapsulated programs, and
independent programs
The main content
 Structures of MATLAB Functions
 Handling Variable Numbers of Arguments
 Debugging of MATLAB Functions
 Pseudo Codes
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.5.1 Structures of MATLAB
Functions
Structures of functions

The numbers of arguments: nargin, nargout


 Comment part: led by %
 The help command
 Input and argument checking allow different ways t
o call CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Generating a Hilbert Matrix

If only one input argument n is given, an


n×n square matrix is generated.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Recursive Calls
Recursive calls: call itself from within itself.
E.g.Solve for factorial n! = n(n-1)! Exits:1!= 0!=1
Exits must be assigned in recursive functions

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.5.2 Handling Variable Numbers of A
rguments
The arguments are saved in varargin 、 varargout
The input arguments are directly accessible with commands
varargin{1},……, varargin{n}
EXAMPLE: multiplication of polynomials
 Polynomials representation: [1,2,3,0,4] vector represent

 Function conv() can only perform multiplication of two polynom


ials
 Multiple polynomials can be handled by using the following
function

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.5.3 Debugging of MATLAB
Functions
Editing and debugging interface
Internal local variables can be debugged and monitore
d
Debugging functions
 Setting breakpoints
 Single step execution
 Moving the single step execution mode into the subfunction.
 Cancelling breakpoints
 Continuing to execute
 Leaving debugging mode
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.5.4 Pseudo Codes
Pseudo codes: holding the function of *.m files, but tr
ansformed into unreadable binary code
Why we need pseudo codes?
 Speeding up the program execution
 Classified codes
Note: source *.m files should be saved in safe place, s
ince the *.p file cannot be converted back.
Commands
 pcode mytest
 pcode mytest –inplace
 pcode *.m
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6 Two-dimensional Graphics in
MATLAB
All two-dimensional graphics supported
Graphics can be created from data or formats
The main content
 Basic two-dimensional graphics
 Plotting functions with other options
 Labeling MATLAB graphics
 Adding texts and other objects to plots
 Other graphics functions with applications
 Plotting implicit functions
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Introduction to "Handle Graphics"

The handle graphics concepts are very useful


in object oriented programming for graphics
processing
 Each element of the graphics, such as axes, curves
and text on the graph, is an independent object.
 These objects can be assigned independently witho
ut affecting other objects.
 Vectorization drawing: each object has a handle
 The objects can be accessed by its handles.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6.1 Basic Two-dimensional Graphics
Known data (experiment point or calculating p
oint)

The two-dimensional "curve" can be easily plo


t

 In fact a set of polylines, not a real curve


 If the points are densely distributed, the polylines c
an look like curves.
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Plot() Function Extention
If t is a vector and y is a matrix, t curves are drawn

If t and y are matrices of the same size, many curves


can be drawn.
Many pairs of such vectors or matrices

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Options in MATLAB Graphics

Double y axis plots with the plotyy() function

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
EXAMPLES
Drawing a sinusoidal curve

Drawing sinusoidal and cosine curves

Drawing double y axis curves

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6.2 Labeling MATLAB Graphics
Modifications of the plots

Setting the range of an axis

Other functions
 text, gtext, xlim, ylim

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6.3 Adding Texts and Other Objects
The user can add texts, arrows and lines to the
graph in graphics window
Modifying the properties of curves
 Line styles, line widths and colors
A subset of LaTeX commands can be used
 Subscripts: x_a, x_{abc}
 Superscript: x^a, x^{abc}
 Font: \bf, \it
 High quality overprint: overpic.sty macro package
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6.4 Other Graphics Functions with A
pplications

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Dividing the Graphics Window

Dividing the graphics window

In each window, plotting functions can be issu


ed independently

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.6.5 Plotting Implicit Functions
Drawing implicit functions ezplot()
Drawing ovals

Complicated implicit function

The parametric equation

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.7 Three-dimensional Graphics

2.7.1 Three-dimensional graphics


Three-dimensional curves
E.g.three-dimensional curves
MATLAB plotting commands

Implicit function plotting commands

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Three-dimensional matchstick curves

Three-dimensional filled up curves

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.7.2 Surface Plots
Three-dimensional surfaces and mesh grid plots can b
e drawn with the functions:

E.g.two-dimensional surfaces

MATLAB commands

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Other functions

The MATLAB command shading

The angle of view setting

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.7.3 Local Processing of Graphics
NaN data will be cut off when plotting
E.g.two variables function

cut off
The MATLAB commands

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8 Graphical User Interface Design in
MATLAB
The graphical user interface determines the quality and
level of the software
The Graphical User Interface technique helps design hi
gh-quality software
The main content
 Graphical User Interface Tool - Guide
 Handle Graphics and Properties of Objects
 Menu System Design
 Illustrative Examples in GUI Design
 Toolbar Design
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8.1 Graphical User Interface
Tool - Guide
Type guide in the MATLAB Commandwindow,
the initial interface starts,and the following option
s shown
 Blank GUI (Default)
 GUI with Uicontrols
 GUI with Axes and Menu
 Modal Question Dialog
 Open Existing GUI

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8.2 Handle Graphics and Properties
of Objects
Graphical User Interface programming mainly aim at extr
acting and setting the properties of objects
A window is an object, and the controls on the window
are also objects, each object has its handle and properties
Double click the objects, and a Property Inspector will be
displayed
Set and get functions are used to set and get properties
The users can modify properties, like color, from the prop
erty browser
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
The Commonly Used Window
Properties
MenuBar property: describes the form of the menu bar of
the proposed window
Name property: sets the title of the current window
Units property: specifies the length unit used in the windo
w property,default options are pixels
Position property: determines the size and position of the
current window
Toolbar property: indicates whether or not to add visible t
oolbars to the window
Visible property: determines whether the window is visib
le or not
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Object Property Extraction
and Setting
Two functions: set() and get()

The commonly used handles


 gcf: get the handle of the current window object
 gco: get the handle of the current object
 gca: get the handle of the current coordinate system
 gcs: get the handle of the current Simulink model
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
EXAMPLE
Design a window: One button and one text box
onto it. When the button is clicked, the “Hello
World!” string will appear in the text box.
 Draw the prototype window: Open a blank prototyp
e window and draw the two control items onto the
window
 Control property modification: the text box control i
tem
String property: set it to an empty one
Tag property: very important, set it to txtHello
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 Automatic generation of the framework of the prog
ram: When the prototype window is designed, it
can be saved into a .fig file, say, to file
c2eggui1.fig.
 Writing callback functions: when the button is clic
ked, operating the text box whose tag is txtHello

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Other callback functions
 CloseRequestFcn:The callback function when a window is
closed
 KeyPressFcn: The callback function when a key in keyboar
d is pressed
 WindowButtonDownFcn: The callback function when a mo
use button is clicked
 WindowButtonMotionFcn:The callback function when the
mouse is moved.
 WindowButtonUpFcn: The callback function when a mous
e button is released.
 CreateFcn and DeleteFcn: The callback functions when an
object is created or deleted
 CallBack: The callback functions when object is selected
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Some of the Commonly Used
Properties for the Control Items
Units and Position properties: the same as the ones
given in window properties
String property: assign a string in the control object
CallBack function: if an object is selected or an action
is done to the object,the callback function can be
executed automatically
Enable property: this indicates whether the control
item is enabled

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
CData property: a true color bitmap is to be drawn on
the control item.
TooltipString property: this stores a string variable
for displaying help information
UserData property: this can be used to store and
exchange information with other controls.
Interruptible property: this indicates whether
interrupts are allowed
The properties related to the font, such as FontAngle,
FontName and so on.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8.4 Menu System Design
The menu editor Tools | Menu Editor
Demo: c2eggui2.m
The users can design menu systems for the wi
ndow
 Adding menu item
 Designing submenu item
 Writing callback function

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8.5 Illustrative Examples in GUI Des
ign
The target: design a window
 A main axis object is needed in the window for use
with three-dimensional graphics
 An edit box is needed for accepting data for plots
 Two push buttons are to be used, one for invoking t
he plot drawing process, and the other for invoking
demonstration facilities.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 A group of three edit boxes, which can be used to
accept the light source position, specified as
coordinates.
 A group of three checkboxes to set whether or not
the grids on the plot axis are displayed, one for
each axis.
 A listbox is needed to allow the user to select differ
ent color shadings.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
A Sketch of the Interface to be Established

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Illustration of Task Assignment for the
Control Objects
The file name: c2eggui3.m

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.8.6 Toolbar Design
Tools | Toolbar Editor
 Standard icons in the toolbar can be used directly
 New icons can also be designed
 How to write a callback function for the icons
Design a new user interface
 Adding frame of axes, drawing the sinusoidal curve
 Adding tolXZoom, tolYZoom buttons
 The file name: c2eggui4.m

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.9 Accelerating MATLAB Functions

Sometimes, MATLAB functions are not effici


ency enough.
How to accelerate?
The main content
 Execution Time and Profiles of MATLAB Functions
 Suggestions for Accelerating MATLAB Functions
 Mex Interface Design

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.9.1 Execution Time and Profiles of
MATLAB Functions
Two sets of commands:
 tic and toc commands
 cputime commands
E.g.Creating a 1000×1000 Hilbert matrix. Then a
singular value decomposition of the matrix is
performed

the two sets of commands are rather close, they


measure 14~15s
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Profile Commands
The profile commands can be used to measure the time
consumed on each statement .Then we can increase the
efficiency

For instance, measuring the CtrlLAB program

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.9.2 Suggestions for Accelerating
MATLAB Functions
1)Avoid loop structures whenever possible
a)Use vectorized computation to replace loops whene
ver possible
E.g.Consider the evaluation of the sum

 The loop structure

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 The vectorized computation

b) If multiple loops are involved, and the execution


time has a significant difference, it is suggested to
have larger loops in the inner loop, and smaller
loops can be used as the outer ones
E.g. Creating a 1000×1000 Hilbert matrix
 General term:
 Double loops: 1:5 and 1:10000, the differences are
obvious

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 Code 1

 Code 2

2) Pre-allocation of large arrays


 Built-in functions such as zeros() and ones()
E.g.Consider again the Hilbert matrix generati
on

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 With the vectorized programming technique,

 meshgrid used to avoid loops

3) Built-in functions should be considered first


 Built-in functions are high-quality functions
4) High efficiency algorithms should be used
 In applications, more efficient algorithms should be adopted
first.
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 quadl() is more effective than quad()
5) Use Mex techniques if possible
 If all the possible MATLAB measures are adopted,and the e
ffectiveness of the program cannot be improved
 With C, Fortran or other languages
 If there is already a professional program developed in
another language, there is no need to rewrite it in MATLAB

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
2.9.3 Mex Interface Design
Compiling programs accroding to the Mex tec
hnical requirement
Dynamic link library files formed in MATLA
B, by compiling links
Accelerating arithmetic speed and the code ca
n be easily reused.
Commonly used methods to call Mex functions
 Find out the data type of a variable

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Class Identifiers Supported in
MATLAB

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
 Get the total number of elements in an input variable

 Measure the size of an input variable

 Check whether a variable is a certain class or not

The header file mex.h should be included


 C compilers supporting 32 bit programming, including
Microsoft Visual C++, Watcom C++ and the LCC-win32
provided inMATLAB, can be used to compile and linkMex
files.
 Compiling and enviroment setup
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Fundamental Structures of Mex Files

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
The Mex interface function is led by the name
mexFunction()

Commonly used functions


 Get the numbers of rows and columns in a matrix.

 Get the pointers of a matrix variable. input and output

 Check whether a matrix is complex or not.

 Dynamical allocation of the pointers of output arguments

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
EXAMPLE
Writing a matrix multiplication in C
 The calling syntax

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
Program Bugs
A result given, though the matrices A and B are mathematicall
y not compatible.(wrong)

Adding dimension tests: c2exmex2a.m


The function still have bugs:
 If either of the two matrices is a scalar, the compatibility check will
give an error message
 If either or both of the two matrices are complex, the whole program w
ould have to be rewritten.
It can be concluded from the above example that, to write a
program in C, you have to consider many tedious things. A
slight carelessness may result in the program being unusable.
CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING
Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
The Application Procedure of the C
Mex Technique
The leading statements: mexFunction() and header functions
The function mxGetPr() can be used to get the pointers of the
input arguments, and the sizes of the input arguments can be
obtained with mxGetM() and mxGetN() functions. The input
variables specified in MATLAB can then be transferred into
the C function.
The function mxCreateDoubleMatrix() can be used to allocate
memory for the returned variables. The function mxGetPr()
can be used to assign pointers to them, so that the returned
variables can be found from MATLAB.

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013
The executable file after compiled: *.mexw32
A .mfile with the same name can be written for on-line help
purposes

CHAPTER 2 FUNDAMENTALS OF MATLAB PROGRAMMING


Dingyu Xue, YangQuan Chen System Simulation Techniques with MATLAB and
12/12/24 18:49 Simulink 2013

You might also like