Configure MATLAB Function Blocks Programmatically - MATLAB & Simulink (original) (raw)

Main Content

You can programmatically adjust and inspect the properties of MATLAB Function blocks. You can also access the content in MATLAB® function reports programmatically.

Call MATLAB Function Objects

After adding a MATLAB Function block to your model, you can use these objects to configure the block:

Programmatically Configure Block Properties

Consider the model call_stats_block2 in Implement MATLAB Functions in Simulink with MATLAB Function Blocks. You can access the MATLABFunctionConfiguration object for the MATLAB Function block in this model by calling the get_param function:

config = get_param("call_stats_block2/MATLAB Function", ... "MATLABFunctionConfiguration");

To query or modify the properties, use dot notation with your object name:

config.Description = "Calculate the mean and standard deviation for a vector of values.";

Access Block Inputs, Outputs, and Properties

To modify the inputs, outputs, and properties of the MATLAB Function block, access its Stateflow.EMChart object by calling the find (Stateflow) function for the Simulink.BlockDiagram object of the current system.

bd = get_param(gcs,"Object"); block = find(bd,"-isa","Stateflow.EMChart", ... Path="call_stats_block2/MATLAB Function");

To query or modify the properties, use dot notation with your object name:

block.Description = "Calculate the mean and standard deviation for a vector of values.";

The Stateflow.EMChart object gives you access to additional properties that are not available in the MATLABFunctionConfiguration object. For example, to create a table of the block inputs and outputs, enter:

info = get([block.Inputs;block.Outputs],{"Name","Scope","Port"}); T = table(info(:,2),cell2mat(info(:,3)), ... VariableNames = ["Scope","Port"], ... RowNames = info(:,1)); T.Scope = categorical(T.Scope)

T =

3×2 table

         **Scope**     **Port**
         ______    ____

**vals**     Input      1  
**mean**     Output     1  
**stdev**    Output     2  

Programmatically Access MATLAB Function Reports

You can access MATLAB function reports by calling these functions onMATLABFunctionConfiguration objects:

For example, to create a custom report that lists the functions and variables in the MATLAB Function block in the call_stats_block2 model, follow these steps:

  1. Access the MATLABFunctionConfiguration object for theMATLAB Function block.
    config = get_param("call_stats_block2/MATLAB Function", ...
    "MATLABFunctionConfiguration");
  2. Create a MATLABFunctionReport object for the MATLAB Function block.
    report = getReport(config);
  3. Access the coder.Function objects in the report.
    functions = report.Functions;
  4. Create a custom report.
    for i = 1:numel(functions)
    fprintf("Function %s uses these variables:\n",functions(i).Name)
    variables = functions(i).Variables;
    for j = 1:numel(variables)
    fprintf("%d. %s -- %s\n",j,variables(j).Name,variables(j).Scope)
    end
    fprintf("\n")
    end
    Function stats uses these variables:
  5. mean -- Output
  6. stdev -- Output
  7. vals -- Input
  8. len -- Local
    Function avg uses these variables:
  9. mean -- Output
  10. array -- Input
  11. size -- Input

Note

The MATLABFunctionReport object does not contain information about errors and warnings. To find errors and warnings in a MATLAB Function block, open the report or use the debugger in the MATLAB Function Block Editor. For more information, see Debug MATLAB Function Blocks.

See Also

Blocks

Functions

Objects

Topics