Main Content

Stateflow.EMChart

Stateflow interface to MATLAB Function block

    Description

    Use Stateflow.EMChart objects to configure MATLAB Function (Simulink) blocks through the Stateflow® programmatic interface.

    MATLAB Function blocks define custom functionality in Simulink® models. 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. See Author Blocks Using MATLAB S-Functions (Simulink).

    For more information, see Implement MATLAB Functions in Simulink with MATLAB Function Blocks (Simulink).

    Tip

    You can also configure the properties of a MATLAB Function block programmatically by using a MATLABFunctionConfiguration (Simulink) object. This object provides a direct interface to the properties of a MATLAB Function block. For more information, see Configure MATLAB Function Blocks Programmatically (Simulink).

    Creation

    Each MATLAB Function block has its own Stateflow.EMChart object. When you add a MATLAB Function block to a Simulink model, a Stateflow.EMChart object is automatically created for it. For example, you can use the function add_block (Simulink) to add a MATLAB Function with the name MATLAB Function to a model called myModel:

    add_block("simulink/User-Defined Functions/MATLAB Function", ...
       "myModel/MATLAB Function")

    Then, to access the Stateflow.EMChart object, call the find function for the Simulink.Root object:

    block = find(sfroot,"-isa","Stateflow.EMChart", ...
        "Path","myModel/MATLAB Function");

    Properties

    expand all

    Stateflow API objects have properties that correspond to the values you set in the Stateflow Editor. To access or modify a property, use dot notation. To access or modify multiple properties for multiple API objects, use the get and set functions, respectively. For more information, see Modify Properties and Call Functions of Stateflow Objects.

    Content

    Name of the MATLAB Function block, specified as a string scalar or character vector.

    Code for the MATLAB Function block, specified as a string scalar or character vector. To enter multiple lines of code, you can:

    • Call the MATLAB function sprintf and use \n to insert newline characters:

      str = sprintf("function y=f(x)\ny=x+1;\nend");
      block.Script = str;
    • Enter a concatenated text expression that uses the function newline to create newline characters:

      str = "function y=f(x)" + newline + ...
          "y=x+1;" + newline + ...
          "end";
      block.Script = str;

    Whether the MATLAB Function block supports variable-size data, specified as a numeric or logical 1 (true) or 0 (false). For more information, see Declare Variable-Size MATLAB Function Block Variables (Simulink).

    Whether the MATLAB Function block supports direct feedthrough semantics, specified as a numeric or logical 1 (true) or 0 (false). For more information, see Allow direct feedthrough (Simulink).

    Whether the MATLAB Function block outputs column vectors as one-dimensional data, specified as a numeric or logical 0 (false) or 1 (true). For more information, see Interpret output column vectors as one-dimensional data (Simulink).

    Since R2023a

    Whether MATLAB Function block output variables with at least one dimension of length 1 are fixed size, specified as a numeric or logical 0 (false) or 1 (true). When this property is true, the object sets variables that are variable size in the block with a dimension of 1 to fixed size. When this property is false, variables in the block that have the Variable size property enabled are always variable size. Prior to R2023a, the object treats variables with at least one dimension of length 1 as fixed size.

    This property only affects output variables that have the Variable size property enabled. See Variable size (Simulink).

    Interface

    This property is read-only.

    Input arguments of the MATLAB Function block, specified as an array of Stateflow.Data objects. The value of this property depends on the inputs defined in the Script property for the block.

    This property is read-only.

    Output arguments of the MATLAB Function block, specified as an array of Stateflow.Data objects. The value of this property depends on the outputs defined in the Script property for the block.

    Discrete and Continuous-Time Semantics

    Activation method for the MATLAB Function block, specified as "CONTINUOUS", "DISCRETE", or "INHERITED". For more information, see Update method (Simulink).

    Sample time for activating the MATLAB Function block, specified as a string scalar or character vector. This property applies only when the ChartUpdate property for the MATLAB function is "DISCRETE".

    Integer and Fixed-Point Data

    Whether the data in the MATLAB Function block saturates on integer overflow, specified as a numeric or logical 1 (true) or 0 (false). When this property is disabled, the data in the function wraps on integer overflow. For more information, see Saturate on integer overflow (Simulink).

    Inherited Simulink signals to treat as Fixed-Point Designer™ fi objects, specified as one of these values:

    • "Fixed-point" — The MATLAB Function block treats all fixed-point inputs as fi objects.

    • "Fixed-point & Integer" — The MATLAB Function block treats all fixed-point and integer inputs as fi objects.

    Default fimath properties for the MATLAB Function block, specified as one of these values:

    • "Same as MATLAB Default" — Use the same fimath properties as the current default fimath object.

    • "Other:UserSpecified" — Use the InputFimath property to specify the default fimath object.

    Default fimath object, specified as a string scalar or character vector. When the EmlDefaultFimath property for the MATLAB Function block is "Other:UserSpecified", you can use this property to:

    • Enter an expression that constructs a fimath object.

    • Enter the variable name for a fimath object in the MATLAB or model workspace.

    Hierarchy

    This property is read-only.

    Machine that contains the MATLAB Function block, specified as a Stateflow.Machine object.

    This property is read-only.

    Location of the MATLAB Function block in the model hierarchy, specified as a character vector.

    Whether the MATLAB Function block has changed after being opened or saved, specified as a numeric or logical 1 (true) or 0 (false).

    Whether the MATLAB Function block is locked, specified as a numeric or logical 1 (true) or 0 (false). Enable this property to prevent changes in the MATLAB Function block.

    This property is read-only.

    Whether the MATLAB Function block is locked, specified as a numeric or logical 1 (true) or 0 (false). This property is equivalent to the property Locked, but is used internally to prevent changes in the MATLAB Function block during simulation.

    Identification

    Description for the MATLAB Function block, specified as a string scalar or character vector.

    Document link for the MATLAB Function block, specified as a string scalar or character vector.

    User-defined tag for the MATLAB Function block, specified as data of any type.

    This property is read-only.

    Unique identifier, specified as an integer scalar. Use this property to distinguish the MATLAB Function block from other objects in the model. The value of this property is reassigned every time you start a new MATLAB session and may be recycled after an object is deleted.

    Object Functions

    findIdentify specified objects in hierarchy
    getChildrenIdentify children of object
    dialogOpen properties dialog box
    viewDisplay object in editing environment

    Examples

    collapse all

    Access the Stateflow.EMChart object for a MATLAB Function block named My Function in a model called myModel.

    block = find(sfroot,"-isa","Stateflow.EMChart", ...
        "Path","myModel/My Function");

    Store the MATLAB code to calculate the mean and standard deviation for a vector of values as a string scalar.

    str = "function [mean,stdev] = stats(vals)" + newline + ...
        "% Calculates a statistical mean and a standard" + newline + ...
        "% deviation for the values in vals." + newline + newline + ...
        "len = length(vals);" + newline + ...
        "mean = avg(vals,len);" + newline + ...
        "stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);" + newline + ...
        "plot(vals,""-+"");" + newline + newline + ...
        "function mean = avg(array,size)" + newline + ...
        "mean = sum(array)/size;";

    Populate the block with code by modifying the Script property of the corresponding Stateflow.EMChart object.

    block.Script = str;

    Open the function in the MATLAB Function Block Editor (Simulink).

    view(block)

    The editor shows this code.

    function [mean,stdev] = stats(vals)
    % 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 mean = avg(array,size)
    mean = sum(array)/size;

    Open a Simulink model called myModel.

    open_system("myModel")

    Add a MATLAB Function block to myModel named My Function.

    blockPath = "myModel/My Function";
    add_block("simulink/User-Defined Functions/MATLAB Function",blockPath)

    Populate the block with code from the MATLAB function myFunction.m.

    block = find(sfroot,"-isa","Stateflow.EMChart", ...
        "Path",blockPath);
    block.Script = fileread("myFunction.m");

    Open a Simulink model called myModel.

    open_system("myModel")

    Find the MATLAB Function blocks in the model, including the block in the library.

    blocks = find(sfroot,"-isa","Stateflow.EMChart");

    Note

    This command finds objects for the MATLAB Function blocks in all open models and libraries. To find only the MATLAB Function blocks in myModel, close every file except myModel or replace sfroot with get_param("myModel","Object").

    Count the number of blocks.

    numel(blocks)

    Version History

    Introduced in R2011a

    expand all