Main Content

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_block1includes 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.

  1. 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.

    this image shows the MATLAB Function Block Editor after it has been opened from the call_stats_block1 model. It includes a default function.

  2. Define the function inputs and outputs by editing the function declaration statement:

    function[mean, stdev] = stats(vals)

    This statement defines a function calledstatswith three variables. The statement defines an input argument,vals, and two output arguments,meanandstdev

  3. 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;
  4. Exit the block. The block updates the port names. The function outputsmeanandstdevcorrespond to block output portsmeanandstdevand the function inputvalscorresponds to the block input portvals

  5. Complete the connections to theMATLAB Functionblock as shown.

    This shows connected signals between the blocks established in the previous image.

  6. Save the model ascall_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 argumentvalsinherits its type and size properties:

  1. Double-click theMATLAB Functionblock.

  2. 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,valsis assigned to an input port.meanandstdevare assigned to output ports. If you change theTypeentry for either variable, the function declaration statement also changes.

  3. Check the properties forvals.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,valsuses 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-3matrix. 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 -setupcommand. 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:

  1. In thecall_stats_block2model, double-click theMATLAB Functionblock.

  2. Open the金宝app仿真软件编码器or Embedded Coder app.

  3. On theC Codetab,Buildto compile and build the example model.

    If you get an error related to theVariable-stepsolver, open the Configuration Parameters window, then clickSolverin the left pane. UnderSolver selection, setTypetoFixed-stepand generate code again. To learn more about the differences between fixed-step and variable-step solvers, seeFixed-Step Versus Variable-Step Solvers

See Also

||

Related Topics