Main Content

实施MATLAB功能金宝appMATLABFunctionBlocks

MATLABFunctionblocks enable you to define custom functions in Simulink®使用MATLAB模型®language.MATLABFunction块支持C/C 金宝app++代码生成金宝appSimulink编码器™and Embedded Coder®

使用这些块时:

  • 您具有现有的MATLAB函数,该函数对自定义功能进行建模,或者您很容易创建此类功能。

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

  • 这custom functionality that you want to model does not include continuous or discrete dynamic states. To model dynamic states, use S-functions. See创建和配置MATLAB S功能

Calculate Mean and Standard Deviation with aMATLABFunctionBlock

此示例以一个包括一个模型开始MATLABFunction阻止并引导您如何自定义块以计算值向量的均值和标准偏差。

打开Model

该模型call_stats_block1包括一个空MATLAB功能块和示例所需的块。打开模型。

Program theMATLABFunctionBlock

编程块来计算值向量的均值和标准偏差。

  1. 打开MATLABFunction Block Editor,双击MATLABFunction堵塞。默认函数出现在MATLABFunction Block Editor和two variables: one input and one output argument.

    此图显示了从Call_stats_block1模型打开的MATLAB功能块编辑器。它包括一个默认函数。

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

    function[平均,stdev] =统计(vals)

    该语句定义了一个称为的函数统计具有三个变量。该语句定义了输入参数,vals和两个输出参数,meanandStdev

  3. 在函数声明语句之后的新行上,添加以下代码:

    % 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,“  - +”);function平均值= avg(数组,大小)均值= sum(array)/size;
  4. Exit the block. The block updates the port names. The function outputsmeanandStdev对应于块输出端口meanandStdevand the function inputvals对应于块输入端口vals

  5. 完成与MATLABFunction如图所示。

    这显示了上图中建立的块之间的连接信号。

  6. Save the model ascall_stats_block2

检查属性MATLABFunction块变量

您可以检查和管理属性的变量MATLABFunction块。看Create and Define MATLAB Function Block Variables。在此示例中,验证输入参数vals继承s its type and size properties:

  1. 双击MATLABFunction堵塞。

  2. 打开符号pane and the Property Inspector. In theFunction选项卡,单击编辑数据

    符号pane displays the variable names in the姓名柱子。这Type列确定函数参数端口分配及其在函数声明语句中出现的位置。在此示例中vals被分配给输入端口。meanandStdev分配给输出端口。如果您更改Typeentry for either variable, the function declaration statement also changes.

  3. Check the properties forvals。在里面符号pane, click the row forvals。这Property Inspector updates to show the properties forvals。这范围property matches theType柱子。

    只能继承一些属性:尺寸,,,,Unit,,,,复杂,,,,andType。Variables indicate that they inherit these properties when尺寸-1,,,,Unit继承,,,,复杂遗传,,,,andType在herit: Same as Simulink。在此示例中valsuses these values for尺寸andType

Check the otherMATLABFunctionblock variables by selecting them in the符号pane.

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 the尺寸将变量指定为一个的属性2 x-3matrix. For more information, see定义和修改变量数据类型and指定MATLAB功能块变量的大小

Build the Function

在输入代码之后MATLABFunctionblock, you can check the code for errors and build the function. SeeDebug MATLAB Function Blocks

当您构建模型时,MATLAB使用编译器来编译您的MATLABfunctionblock code. If you have multiple MATLAB-supported compilers installed on your system, you can change the default compiler using themex -setup命令。看更改默认编译器。To see a list of supported compilers, openSupported and Compatible Compilers,单击与您的操作系统相对应的选项卡,找到金宝appSimulink产品家族桌子,去For Model Referencing, Accelerator mode, Rapid Accelerator mode, and MATLAB Function blocks柱子。

在Si金宝appmulink中,单击模拟模型。该软件构建了MATLABFunction当它构建模型并将数据从函数输出到两个时阻止展示块。

准备代码生成的块

从包含的模型中生成独立的C/C ++代码MATLABFunction块,使用金宝appSimulink编码器或嵌入式编码器。

选择一个支持的编译金宝app器进行仿真和代码生成

To see a list of supported compilers, openSupported and Compatible Compilers,单击与您的操作系统相对应的选项卡,找到金宝appSimulink产品家族桌子,去金宝appSimulink编码器柱子。

生成代码MATLABFunctionBlock

如果您有许可金宝appSimulink编码器or Embedded Coder, you can generate code from the model. To generate code:

  1. 在里面call_stats_block2model, double-click theMATLABFunction堵塞。

  2. 打开金宝appSimulink编码器or Embedded Coder app.

  3. C Code标签,Build到compile and build the example model.

    如果您发现与Variable-stepsolver, open the Configuration Parameters window, then clickSolver在左窗格中。在下面Solver selection, 放Type固定步骤并再次生成代码。要了解有关固定步骤和可变步骤求解器之间差异的更多信息,请参见固定步骤与可变步骤求解器

也可以看看

||

Related Topics