ImplementMATLABFunctions in金宝appwithMATLAB FunctionBlocks
MATLAB Functionblocks enable you to define custom functions in Simulink®models by using the MATLAB®language.MATLAB Functionblocks support C/C++ code generation from金宝app仿真软件编码器™and Embedded Coder®.
Use these blocks when:
You have an existing MATLAB function that models custom functionality, or it is easy for you to create such a function.
Your model requires custom functionality that is not or cannot be captured in the Simulink graphical language.
You find it easier to model custom functionality by using a MATLAB function than by using a Simulink block diagram.
The custom functionality that you want to model does not include continuous or discrete dynamic states. To model dynamic states, use S-functions. SeeCreate and Configure MATLAB S-Functions.
Calculate Mean and Standard Deviation with aMATLAB FunctionBlock
This example starts with a model that includes aMATLAB Functionblock and guides you through how to customize the block to calculate the mean and standard deviation for a vector of values.
Open the Model
The modelcall_stats_block1
includes an empty MATLAB Function block and the blocks you need for the example. Open the model.
Program theMATLAB FunctionBlock
Program the block to calculate the mean and standard deviation for a vector of values.
To open theMATLAB Function Block Editor, double-click theMATLAB Functionblock. A default function appears in theMATLAB Function Block Editorwith two variables: one input and one output argument.
Define the function inputs and outputs by editing the function declaration statement:
function[mean, stdev] = stats(vals)
This statement defines a function called
stats
with three variables. The statement defines an input argument,vals
, and two output arguments,mean
andstdev
.On a new line after the function declaration statement, add the following code:
% Calculates a statistical mean and a standard% deviation for the values in vals.len = length(vals); mean = avg(vals,len); stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len); plot(vals,"-+");functionmean = avg(array,size) mean = sum(array)/size;
Exit the block. The block updates the port names. The function outputs
mean
andstdev
correspond to block output portsmeanandstdevand the function inputvals
corresponds to the block input portvals.Complete the connections to theMATLAB Functionblock as shown.
Save the model as
call_stats_block2
.
Check Properties forMATLAB FunctionBlock Variables
You can check and manage properties for variables inMATLAB Functionblocks. SeeCreate and Define MATLAB Function Block Variables.在这个例子中,verify that the input argumentvals
inherits its type and size properties:
Double-click theMATLAB Functionblock.
Open theSymbolspane and the Property Inspector. In theFunctiontab, clickEdit Data.
TheSymbolspane displays the variable names in theNamecolumn. TheTypecolumn determines the function argument port assignment and where it appears in the function declaration statement. In this example,
vals
is assigned to an input port.mean
andstdev
are assigned to output ports. If you change theTypeentry for either variable, the function declaration statement also changes.Check the properties for
vals
.In theSymbolspane, click the row forvals
.The Property Inspector updates to show the properties forvals
.TheScopeproperty matches theTypecolumn.Only some properties can be inherited:Size,Unit,Complexity, andType.Variables indicate that they inherit these properties whenSizeis
-1
,Unitisinherit
,ComplexityisInherited
, andTypeisInherit: Same as Simulink
.In this example,vals
uses these values forSizeandType.
Check the otherMATLAB Functionblock variables by selecting them in theSymbolspane.
By default, newly defined input and output variables inherit their size, unit, complexity, and type. If you want the variables to use a specific size, unit, or type, you can specify these properties explicitly. For example, enter[2 3]
in theSizeproperty to specify the variable as a2-by-3
matrix. For more information, seeDefine and Modify Variable Data TypesandSpecify Size of MATLAB Function Block Variables.
Build the Function
在你输入代码MATLAB Functionblock, you can check the code for errors and build the function. SeeDebug MATLAB Function Blocks.
When you build a model, MATLAB uses a compiler to compile yourMATLAB functionblock code. If you have multiple MATLAB-supported compilers installed on your system, you can change the default compiler using themex -setup
command. SeeChange Default Compiler.To see a list of supported compilers, openSupported and Compatible Compilers, click the tab that corresponds to your operating system, find theSimulink Product Familytable, and go to theFor Model Referencing, Accelerator mode, Rapid Accelerator mode, and MATLAB Function blockscolumn.
In Simulink, clickRunto simulate the model. The software builds theMATLAB Functionblock when it builds the model and outputs the data from the function to the twoDisplayblocks.
Prepare the Block for Code Generation
To generate standalone C/C++ code from a model that containsMATLAB Functionblocks, use金宝app仿真软件编码器or Embedded Coder.
Select a Supported Compiler for Simulation and Code Generation Builds
To see a list of supported compilers, openSupported and Compatible Compilers, click the tab that corresponds to your operating system, find theSimulink Product Familytable, and go to the金宝app仿真软件编码器column.
Generate Code for theMATLAB FunctionBlock
If you have a license for金宝app仿真软件编码器or Embedded Coder, you can generate code from the model. To generate code:
In the
call_stats_block2
model, double-click theMATLAB Functionblock.Open the金宝app仿真软件编码器or Embedded Coder app.
On theC Codetab,Buildto compile and build the example model.
If you get an error related to the
Variable-step
solver, open the Configuration Parameters window, then clickSolverin the left pane. UnderSolver selection, setTypetoFixed-step
and generate code again. To learn more about the differences between fixed-step and variable-step solvers, seeFixed-Step Versus Variable-Step Solvers.
See Also
MATLAB Function|MATLAB Function Block Editor|add_block