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:

    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 Simulink.Root object:

    rt = sfroot;
    block = find(rt,'-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 (Stateflow).

    Content

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

    Code for the MATLAB Function block, specified as a 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 integer 10 as the ASCII equivalent of a newline character:

      str = ['function y=f(x)',10, ...
          'y=x+1;',10, ...
          'end'];
      block.Script = str;

    Whether the MATLAB Function block supports variable-size data, specified as a numeric or logical 1 (true) or 0 (false). Only variable-size data can change dimension during simulation. For more information, see Declare Variable-Size Inputs and Outputs.

    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.

    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.

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

    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 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 character vector.

    Document link for the MATLAB Function block, specified as a 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.

    block = find(sfroot,'-isa','Stateflow.EMChart');

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

    str = ['function [mean,stdev] = stats(vals)',10, ...
    '% Calculates a statistical mean and a standard',10, ...
    '% deviation for the values in vals.',10,10, ...
    'len = length(vals);',10, ...
    'mean = avg(vals,len);',10, ...
    'stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);',10, ...
    'plot(vals,''-+'');',10,10,...
    'function mean = avg(array,size)',10, ...
    '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.

    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',newBlockPath);
    block.Script = fileread('myFunction.m');

    Open a Simulink model called myModel.

    open_system('myModel')

    Access the Simulink.Root object at the top level of the Stateflow hierarchy.

    rt = sfroot;

    Find the MATLAB Function blocks in the model.

    blocks = find(rt,'-isa','Stateflow.EMChart');

    Count the number of blocks.

    numel(blocks);
    Introduced in R2011a