Configure MATLAB Function Blocks Programmatically
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:
Use a
MATLABFunctionConfigurationobject to query and modify the properties of the block. Identify the block by using a block path or calling thegcbfunction.Use a
Stateflow.EMChartobject to access the inputs, outputs, and properties of the block. Identify the block by navigating the hierarchy of Stateflow® API objects.
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.UpdateMethod
ans = 'Inherited'
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.ChartUpdate
ans = 'INHERITED'
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 on
MATLABFunctionConfiguration objects:
openReportopens the MATLAB function report for the block.closeReportcloses the MATLAB function report for the block.getReportreturns aMATLABFunctionReportobject for the block. You can query report information from this object by accessing itsFunctionsproperty, which is an array ofcoder.Functionobjects. See coder.Function Properties (MATLAB Coder).
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:
Access the
MATLABFunctionConfigurationobject for the MATLAB Function block.config = get_param("call_stats_block2/MATLAB Function", ... "MATLABFunctionConfiguration");
Create a
MATLABFunctionReportobject for the MATLAB Function block.report = getReport(config);
Access the
coder.Functionobjects in the report.functions = report.Functions;
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: 1. mean -- Output 2. stdev -- Output 3. vals -- Input 4. len -- Local Function avg uses these variables: 1. mean -- Output 2. array -- Input 3. 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
- Overview of the Stateflow API (Stateflow)