Main Content

Stateflow.EMChart

Stateflow interface to MATLAB Function block

    Description

    Use Stateflow.EMChart objects to configure MATLAB Function blocks through the Stateflow® programmatic interface. For more information, see Overview of the Stateflow API (Stateflow).

    Tip

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

    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 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 (Stateflow) function for the loaded system:

    block = find(get_param("myModel","Object"), ...
        "-isa","Stateflow.EMChart","Name","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 (Stateflow).

    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.

    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.

    Since R2021b

    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.

    Since R2023a

    Whether the MATLAB Function block treats output variables with a dimension of length 1 as fixed size, specified as a numeric or logical 0 (false) or 1 (true). When this property is true, the MATLAB Function block treats output variables that have at least one dimension of length 1 as fixed size, regardless of whether you specify the variables as having variable size. When this property is false, the MATLAB Function block treats output variables as variable size if you specify the variables as having variable size. For more information, see Variable size.

    Interface

    This property is read-only.

    Input arguments of the MATLAB Function block, specified as an array of Stateflow.Data (Stateflow) 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 (Stateflow) 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.

    Sample time for activating the MATLAB Function block, specified as a string scalar or character vector. This property applies only when the ChartUpdate property 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.

    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 (Stateflow) 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

    find (Stateflow)Identify specified objects in hierarchy
    getChildren (Stateflow)Identify children of object
    dialog (Stateflow)Open properties dialog box
    view (Stateflow)Display 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(get_param(gcs,"Object"), ...
        "-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.

    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(get_param("myModel","Object"),"-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.

    blocks = find(get_param("myModel","Object"),"-isa","Stateflow.EMChart");

    Count the number of blocks.

    numel(blocks)

    Version History

    Introduced in R2011a

    expand all