Professional Documents
Culture Documents
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
Home
Financial Engineering
Option Pricing
Econometrics
Portfolio Optimization
Technical Analysis
Software Tutorials
MATLAB
Simulink
Note that in release R2011a several blocks where renamed (although their functionality
VBA
did not change). The Embedded MATLAB Function block was one such block.
C++
Services
Financial Modeling
Control System Design
Generic Modeling
Auditing Code
Software Training
About Us
Contact Us
Note that pre- and post-R2011a there ia a block called the MATLAB Function Block, but
it behaves very differently depending on the version of MATLAB being used.
This tutorial will refer to the pre-R2011a name: Embedded MATLAB Function Block.
The tutorial is split into the following sections,
1 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
2 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
The input and output arguments of an Embedded MATLAB Function are interpreted in a
special way. The functions number of input arguments automatically corresponds to the
number of block input ports and the functions number of output arguments automatically
corresponds to the number of block output ports. Similarly the input arguments
automatically take on the size and data-type of input signals, and the output signals
automatically take on the size and data-type of the output variables created in the
function. This imposes the restriction that the size and data-type of output variables
typically needs to be defined at the start of the m-code and not changed during the
simulation.
Debugging an Embedded MATLAB Function Block is performed in exactly the same way
as the debugging of a standard MATLAB function. Break-points may be inserted at any
line in the code (within the editor), and when the model is executed the simulation will
stop each time any line with a breakpoint is encountered. All of the usual ability to step
through the code and inspect and/or change variables is available.
The Embedded MATLAB Function block works by converting the written m-code into an
S-Function. (For a discussion of S-Functions see the Writing Simulink S-Functions
tutorials.) All parts of the code that use a certain subset of the MATLAB language called the Embedded Subset - are converted to C and compiled. All other parts of the
code (i.e. the parts that use MATLAB code that is not part of the Embedded Subset) are
executed by calling MATLABs (interpretive) engine in the usual way.
To view the list of functions that are in the Embedded MATLAB Subset type the following
at the MATLAB command line
>> docsearch('Embedded MATLAB Function Library Alphabetical List');
To assist the parser in splitting the code into the two types (Embedded Subset and not
Embedded Subset), all MATLAB functions called from within the code that are not part of
the Embedded Subset must be specified as such. This is achieved using the function
eml.extrinsic near the top of the Embedded MATLAB function. An example of using
eml.extrinsic is given in the Example Model and M-Code section below.
3 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
When a block contains only m-code from the Embedded Subset the block may be used
with Simulink Coder (formerly Real-Time Workshop) to generate standalone and/or
embeddable code.
Although very convenient to use, there is a drawback to the Embedded MATLAB
Function Block. That is that it does not have any capability to segment the code into
individual parts that perform just ininitialization, simulation, or termination. All the code
gets executed at every simulation time step. When special initialization and/or termination
code is required then an S-Function is recommended.
4 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
% We're going to use the MATLAB function called sort to do the sorting.
% Since it isn't in the Embedded Subset of MATLAB it must be defined as
% being extrinsic
eml.extrinsic('sort');
% Ensure the outputs are the same size and data-type as the input
vOut = vIn;
iOut = vIn;
% Perform the sort operation
[vOut,iOut] = sort(vIn,'descend');
5 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
%#eml
% Function to implement a vector sort algorithm.
% Used to sort the input vector into descending order and output the
% sorted vector along with the indices showing how the sort was achieved.
%
% This is NOT a bubble sort algorithm.
% It is orders of magnitude father than bubble sorting for vectors
% with more than about 10 elements.
6 of 7
3/26/2013 10:48 AM
http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html
end
% If any are bigger then do a swap
if (maxIndex~=idx)
vOut(maxIndex) = vOut(idx);
iOut(maxIndex) = iOut(idx);
vOut(idx) = vMax;
iOut(idx) = iMax;
end
end
7 of 7
3/26/2013 10:48 AM