zrwangems/dsh-zrwang-plugin / skills/engineering/simulink/simulink-modeling/simulink-integrate-python-code
simulink-integrate-python-code
UnverifiedUse this skill when running or integrating general Python code (functions, classes, libraries) inside a Simulink model with the Python Code block (R2025a+). Not for trained neural-network models (.pt, .onnx, .h5) or deep-learning inference; use the Deep Learning Toolbox Model Predict blocks or network importers. Covers the SymbolSpec API, data-type mapping, persistent state, shared interpreter, venvs, and module reloading. Triggers: "Python Code block", "Python in Simulink", "integrate Python", "call Python from Simulink", "run my Python function/class in a model", "add Python to model", "use a Python library in a simulation".
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.3 KB |
| manifest.yaml | config | 581 B |
| references/environment.md | markdown | 3.1 KB |
| references/examples.md | markdown | 3.8 KB |
| references/pythonobject-patterns.md | markdown | 2.1 KB |
references/pythonobject-patterns.md
View on GitHub# PythonObject Patterns
## Integrating External Systems and Classes
Primary use case: instantiate a Python class or library object in Start, persist
it as `PythonObject`, call methods each timestep in Output.
1. Define a `Persistent` symbol with `Type = 'PythonObject'`
2. `InitializeConditionsCode`: import and instantiate
3. `OutputCode`: call `.step()`, `.update()`, etc.
4. `TerminateCode`: clean up if needed
Only the `Persistent` instance and imported modules reach `OutputCode` — names
from `from m import X` are init-only, so call methods on the instance.
**External system (battery model, physics sim, controller, etc.):**
```python
# Start
from my_package import MySystem
system = MySystem(dt=0.01, param_a=9.81)
```
```python
# Output
result = system.step(input_signal)
y = result.output_value
```
**Third-party library (e.g., PyBaMM battery simulation):** `sim` is a
`Persistent` `PythonObject`, `dt` a `Parameter`. PyBaMM only accepts `inputs=`
for parameters set to `"[input]"`.
```python
# Start
import pybamm
model = pybamm.lithium_ion.SPMe()
params = model.default_parameter_values
params["Current function [A]"] = "[input]"
sim = pybamm.Simulation(model, parameter_values=params)
```
```python
# Output
sim.step(dt=dt, inputs={"Current function [A]": current_in})
voltage_out = float(sim.solution["Voltage [V]"].entries[-1])
```
## General-Purpose Persistent State
`PythonObject` stores any Python object across timesteps — not limited to classes.
Useful when data does not map to Simulink numeric types.
**Config dict** — pack many parameters into one symbol:
```python
# Start
config = {'alpha': 0.5, 'beta': 0.3, 'threshold': 100, 'mode': 'fast'}
```
```python
# Output
y = x * config['alpha'] + config['beta']
```
**Rolling window:**
```python
# Start
from collections import deque
history = deque(maxlen=100)
```
```python
# Output
history.append(sensor_val)
y = sum(history) / len(history)
```
Other examples: compiled regexes, database connections, caches, protocol buffers,
accumulators — anything you would store in a Python variable.
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 464k | Updated | Claude CodeCodex | Verified | Install |
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 464k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 318k | Updated | Claude CodeCodex | Unverified | Install |