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:
- Use a MATLABFunctionConfiguration object to query and modify the properties of the block. Identify the block by using a block path or calling the gcb function.
- Use a Stateflow.EMChart object 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.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:
- openReport opens the MATLAB function report for the block.
- closeReport closes the MATLAB function report for the block.
- getReport returns a MATLABFunctionReport object for the block. You can query report information from this object by accessing its
Functions
property, which is an array ofcoder.Function
objects. 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
MATLABFunctionConfiguration
object for theMATLAB Function block.
config = get_param("call_stats_block2/MATLAB Function", ...
"MATLABFunctionConfiguration"); - Create a
MATLABFunctionReport
object for the MATLAB Function block.
report = getReport(config); - Access the
coder.Function
objects 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: - mean -- Output
- stdev -- Output
- vals -- Input
- len -- Local
Function avg uses these variables: - mean -- Output
- array -- Input
- 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)