Main Content

ReuseMATLABCode by DefiningMATLABFunctions

A MATLAB®function in a Stateflow®chart is a graphical element that you use to write algorithms that are easier to implement by calling built-in MATLAB functions. Typical applications include:

  • Matrix-oriented calculations

  • 数据分析和可视化

This type of function is useful for coding algorithms that are more easily expressed by using MATLAB instead of the graphical Stateflow constructs. MATLAB functions also provide optimizations for generating efficient, production-quality C code for embedded applications.

MATLAB功能可以驻留在图表,状态或子图中的任何地方。函数的位置确定可以调用该函数的状态和过渡集。

  • 如果你想在o调用该函数ne state or subchart and its substates, put your MATLAB function in that state or subchart. That function overrides any other functions of the same name in the parents and ancestors of that state or subchart.

  • If you want to call the function anywhere in a chart, put your MATLAB function at the chart level.

  • If you want to call the function from any chart in your model, put your MATLAB function at the chart level and enable exporting of chart-level functions. For more information, seeExport Stateflow Functions for Reuse.

Note

A MATLAB function can access chart and state data above it in the Stateflow hierarchy.

For example, this MATLAB function has the namestdevstats. It takes an argumentvals并返回一个输出luestdevout.

To compute the standard deviation of the values invals, the function uses this code.

functionstdevout = stdevstats(vals)%#codegen% Calculates the standard deviation for valslen = length(vals); stdevout = sqrt(sum(((vals-avg(vals,len)).^2))/len);functionmean = avg(array,size) mean = sum(array)/size;

Define aMATLABFunction in a Chart

  1. In the object palette, click the MATLAB function icon. Move your pointer to the location for the new MATLAB function in your chart.

  2. Enter the signature label for the function.

    功能签名标签指定您的函数名称以及其参数和返回值的正式名称。A signature label has this syntax:

    [return_val1,return_val2,...] = function_name(arg1,arg2,...)
    You can specify multiple return values and multiple input arguments. Each return value and input argument can be a scalar, vector, or matrix of values. For functions with only one return value, omit the brackets in the signature label.

    You can use the same variable name for both arguments and return values. For example, a function with this signature label uses the variablesy1andy2as both inputs and outputs:

    [y1,y2,y3] = f(y1,u,y2)
    If you export this function to C code,y1andy2are passed by reference (as pointers), anduis passed by value. Passing inputs by reference reduces the number of times that the generated code copies intermediate data, resulting in more optimal code.

    Note

    Do not use the name of a chart symbol as a function argument or return value. For example, if eitherxoryare chart data, defining a function with the signaturey = f (x)results in a run-time error.

  3. To program the function, open the MATLAB editor by double-clicking the function box.

  4. In the MATLAB editor, enter the MATLAB code implementing your function. For more information, seeProgram a MATLAB Function in a Chart.

  5. In the Model Explorer, expand the chart object and select the MATLAB function. The arguments and return values of the function signature appear as data items that belong to your function. Arguments have the scopeInput. Return values have the scopeOutput.

  6. In the Data properties dialog box for each argument and return value, specify the data properties, as described inSet Data Properties.

  7. Create any additional data items required by your function. For more information, seeAdd Data Through the Model Explorer.

    Your function can access its own data or data belonging to parent states or the chart. The data items in the function can have one of these scopes:

    • Constant— Constant data retains its initial value through all function calls.

    • Parameter- 参数数据通过所有函数调用保留其初始值。

    In MATLAB functions, you do not need to create local or temporary function data explicitly. If you use an undefined variable, Stateflow creates a temporary variable that is available to the rest of the function. To store values that persist across function calls, use the keywordpersistent.

CallMATLABFunctions in States and Transitions

You can call MATLAB functions from the actions of any state or transition. You can also call MATLAB functions from other functions. If you export a MATLAB function, you can call it from any chart in the model.

To call a MATLAB function, use the function signature and include an actual argument value for each formal argument in the function signature. If the data types of the actual and formal arguments differ, the function casts the actual argument to the type of the formal argument.

Specify Properties ofMATLABFunctions

You can set general properties for your MATLAB function through its properties dialog box. To open the function properties dialog box, right-click the MATLAB function box and selectPropertiesfrom the context menu.

Name

Function name. Click the function name link to open your function in the MATLAB editor.

Function Inline Option

Controls the inlining of your function in generated code:

  • Auto— Determines whether to inline your function based on an internal calculation.

  • Inline— Inlines your function if you do not export it to other charts and it is not part of a recursion. (A recursion exists if your function calls itself directly or indirectly through another function call.)

  • Function— Does not inline your function.

Label

您的功能的签名标签。功能签名标签指定您的函数名称以及其参数和返回值的正式名称。

Saturate on Integer Overflow

Specifies whether integer overflows saturate in the generated code. For more information, seeHandle Integer Overflow for Chart Data.

MATLABFunctionfimath

Defines thefimathproperties for the MATLAB function. Thefimathproperties specified are associated with allfiandfimathobjects constructed in the MATLAB function. Choose one of these options:

  • Same as MATLAB— The function uses the samefimathproperties as the current globalfimath. The edit box appears dimmed and displays the current globalfimathin read-only form. For more information on the globalfimathandfimathobjects, see the Fixed-Point Designer™ documentation.

  • Specify Other— Specify your ownfimathobject by one of these methods:

    • Construct thefimathobject inside the edit box.

    • Construct thefimathobject in the MATLAB or model workspace and enter its variable name in the edit box.

Description

Function description. You can enter brief descriptions of functions in the hierarchy.

Document Link

Link to online documentation for the function. You can enter a web URL address or a MATLAB command that displays documentation in a suitable online format, such as an HTML file or text in the MATLAB Command Window. When you click theDocument linkhyperlink, Stateflow displays the documentation.

相关话题