brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
simulink-use-c-function-blockUse when integrating legacy code, custom code, or C/C++ code into Simulink via C Function blocks. Configure Simulink C Function blocks programmatically using the SymbolSpec API. TRIGGER when the user: - Asks about calling or wrapping C/C++ code or libraries in Simulink - Wants to create, configure, or use a C Function block - Mentions integrating custom cod…
Independent GitHub snapshot · Automated classification · Original documentation governs
name: simulink-use-c-function-block description: | Use when integrating legacy code, custom code, or C/C++ code into Simulink via C Function blocks. Configure Simulink C Function blocks programmatically using the SymbolSpec API. TRIGGER when the user:
float → single, bool → boolean, char*/char[] → string (R2024b+)). However, when replacing an existing block (e.g. an S-Function), use the types already on that block first — for example, what looks like int32 from C may actually be fixdt(1,32,3) or another fixed-point type in the existing model.Class: ClassName and call class methods directly in the code sections (e.g. obj.Method(args)). If the class has an init method (not a constructor), call it in StartCode. Only fall back to wrapper/handler functions or opaque pointer patterns if the class approach fails to compile.'myFunctions.h', './src/myLib.cpp') rather than absolute paths. This ensures portability across machines and users.model_edit with type "C Function" and set CustomCodeSettingLocation (default: "BlockSettings")references/custom-code-config.md)OutputCode, StartCode, etc. on the blockmodel_query_params to get the SymbolSpec, then evaluate_matlab_code for object operationsThe SymbolSpec object defines the block's inputs, outputs, persistent state, constants, and parameters.
Query with model_query_params, then operate via evaluate_matlab_code:
obj = get_param('myModel/MyCFunction', 'SymbolSpec');
symObj = obj.addSymbol('varName'); % adds with default: Input, double, size '1'
symObj = obj.getSymbol('varName'); % get one by name
allSyms = obj.Symbols; % get all symbols
obj.deleteSymbol('varName');
Note: addSymbol always creates with defaults (double, Input, size '1'). When re-adding a deleted symbol, explicitly set all non-default properties (Type, Scope, Size) — prior properties are not retained.
| Property | Values | Default | Notes |
|---|---|---|---|
Name | identifier string | (from addSymbol) | Variable name in code; for class types includes constructor args |
Scope | 'Input', 'Output', 'InputOutput', 'Persistent', 'Constant', 'Parameter' | 'Input' | See Scope Details below |
Type | 'double', 'single', 'int8'...'uint64', 'Boolean', 'string', 'Bus: BusName', 'Enum: EnumName', 'Class: ClassName', 'AliasTypeName', or fixedpoint | 'double' | |
Size | dimension string | '1' | e.g. '1', '3', '[2,3]', 'size(u1)' |
Label | port label / value | same as Name | For Constant: the literal value |
PortNumber | uint32 | auto-assigned | Port ordering |
| Scope | Description | Port? |
|---|---|---|
Input | Block input port | Yes (inport) |
Output | Block output port | Yes (outport) |
InputOutput | Read-write pass-through (always first in port numbering) | Yes (both) |
Persistent | State that persists across time steps (also used for class instances) | No |
Constant | Compile-time constant (scalar only) | No |
Parameter | Tunable parameter from workspace/mask | No |
After setting Scope to 'Parameter', set the value by configuring the block with the parameter name as the key.
Class instances are declared as Persistent scope symbols with Type = 'Class: ClassName'.
Constructor arguments are specified in the Name property:
obj = get_param('myModel/CFunction', 'SymbolSpec');
% Default constructor (no args)
sym = obj.addSymbol('myObj');
sym.Scope = 'Persistent';
sym.Type = 'Class: MyClass';
% Constructor with arguments (args must be Parameter or Constant scope symbols)
sym = obj.addSymbol('myObj(p)');
sym.Scope = 'Persistent';
sym.Type = 'Class: MyClass';
Rules for class types:
CustomCodeSettingLocation = 'ModelConfigurationParameters' (always parsed), or 'BlockSettings' with ParseBlockCode = 'on'new operator is not allowed when block code is parsedAccess class methods in code sections: y = myObj.compute(u1);
'1''3' or '[3,1]''[2,3]''size(u1)', 'size(u1,1)', 'size(u1) + 1'Rules:
size() IS allowed in output dimensions (compute output size from inputs)size() is NOT allowed in input dimensions'-1' (inherited) — must be explicit or use size() expression'-1' (inherited)size(size(...)) is NOT supported#include directives NOT allowed inside code sections (Start/Output/Terminate) — use custom code settingsisalnum) not supported inside code sections when ParseBlockCode = 'on' or CustomCodeSettingLocation = 'ModelConfigurationParameters' — use external calls via header. No restriction under default R2026a+ settings (BlockSettings + ParseBlockCode = 'off')Simulink.NumericType with DataScope = 'Exported' in symbols, or any Simulink.NumericType in code sections, its IsAlias property must be trueCustomCodeSettingLocation = 'UpdateBuildInfo', GenerateCodeAsIs is forced 'on'% 1. Add C Function block with block-level custom code
% Use model_edit: add_block type "C Function", set CustomCodeSettingLocation,
% SimCustomHeaderFile, SimCustomSourceFile, and OutputCode on the block.
% 2. Configure symbols
obj = get_param('mMyTest/C Function', 'SymbolSpec');
obj.addSymbol('u1');
obj.addSymbol('u2');
u2obj = obj.getSymbol('u2');
u2obj.Type = 'int32';
u2obj.Size = '3';
obj.addSymbol('y');
yobj = obj.getSymbol('y');
yobj.Scope = 'Output';
yobj.Size = 'size(u2)';
obj.addSymbol('state');
stateobj = obj.getSymbol('state');
stateobj.Scope = 'Persistent';
save_system('mMyTest');
close_system('mMyTest', 0);
See references/examples.md for additional patterns (model-level custom code, C++ class with constructor args).
See references/custom-code-config.md for detailed parameter tables and custom code location settings.
Copyright 2026 The MathWorks, Inc.