simulating-simulink-models

$npx mdskill add matlab/simulink-agentic-toolkit/simulating-simulink-models

Runs Simulink models programmatically for data exploration and analysis.

  • Enables parameter sweeps and custom analysis via simulation results.
  • Depends on sim(), SimulationInput, SimulationOutput, and related APIs.
  • Uses SimulationInput to configure model parameters, inputs, and variables.
  • Returns SimulationOutput objects with logged signals and simulation metadata.

SKILL.md

.github/skills/simulating-simulink-modelsView on GitHub ↗
---
name: simulating-simulink-models
description: Runs Simulink models programmatically for data exploration, parameter sweeps, and custom analysis using sim() with SimulationInput/SimulationOutput. Use when calling sim(), parsim, setExternalInput, setModelParameter, setVariable, or accessing logsout — any task producing simulation results for analysis (not pass/fail tests).
license: MathWorks BSD-3-Clause
metadata:
  author: MathWorks
  version: "1.1"
---

# Simulating Simulink Models with the sim Command

Use this skill to generate simulation results for analysis. For persistent, reusable pass/fail behavioral testing (especially of individual subsystems), use `testing-simulink-models` instead.

## When to Use

- Running a Simulink model from a MATLAB script
- Configuring simulation parameters (StopTime, solver, etc.) programmatically
- Passing input signals to root-level Inport blocks
- Accessing logged signal data after simulation
- Running parameter sweeps or batch simulations

## When NOT to Use

- Writing declarative Gherkin-based tests → use `testing-simulink-models`
- Testing an individual subsystem or component → use `testing-simulink-models` (requires Simulink Test; auto-creates a harness, compiles only the subsystem — much faster than `sim()` which always compiles the entire model)

## Minimal working pattern

Always simulate using `Simulink.SimulationInput` and `Simulink.SimulationOutput`:

```matlab
in = Simulink.SimulationInput('MyModel');
in = in.setModelParameter('StopTime', '10');
out = sim(in);
```

## Setting parameters

Use `SimulationInput` methods to configure the simulation:

```matlab
% Model-level parameters (StopTime, SolverType, SimulationMode, etc.)
in = in.setModelParameter('StopTime', '10', 'SolverType', 'Fixed-step');

% Block parameters — resolve path from blk_X ID (never type block names manually)
blkPath = Simulink.ID.getFullName('MyModel:5');
in = in.setBlockParameter(blkPath, 'Gain', '5');

% MATLAB workspace variables used by the model
in = in.setVariable('Kp', 1.2);
```

## Input signals

Pass input signals through Inport blocks using a `Simulink.SimulationData.Dataset`. Elements are matched to Inport blocks **by index position** — the first element maps to the Inport with port number 1, the second to port number 2, and so on.

```matlab
dt = 0.01;
N = 1000;
t = dt*(0:N)';
u = sin(2*pi*t);

ts = timeseries(u, t);

ds = Simulink.SimulationData.Dataset;
ds{1} = ts;

in = in.setExternalInput(ds);
out = sim(in);
```

You can also use `timetable` as an input format:

```matlab
secs = seconds(t);
tt = timetable(secs, u);

ds = Simulink.SimulationData.Dataset;
ds{1} = tt;

in = in.setExternalInput(ds);
```

## Discovering logged data

First, discover what kinds of logged data the model produces using `who`, then inspect signal names within `logsout`:

```matlab
in = Simulink.SimulationInput('MyModel');
out = sim(in);

% See what logging properties exist (logsout, yout, tout, etc.)
who(out)

% List individual signal names within logsout
disp(out.logsout.getElementNames);
```

## Accessing logged data

Logged signals are available through `out.logsout`. Access them directly by name:

```matlab
% Plot a logged signal
plot(out.logsout.get('signalName').Values)

% Get time and data separately
sig = out.logsout.get('signalName').Values;
plot(sig.Time, sig.Data)
```

## Multiple simulations

When running many simulations, create an array of `Simulink.SimulationInput` objects:

```matlab
in = repmat(Simulink.SimulationInput('MyModel'),N,1);
for k = 1:N
    in(k) = Simulink.SimulationInput('MyModel');
    in(k) = in(k).setVariable('gain', gains(k));
end
out = sim(in);
```

To enable fast restart for iterative sweeps (compiles the model only once):

```matlab
out = sim(in, 'UseFastRestart', 'on');
```

## Parallel simulation (parsim)

To run multiple simulations in parallel, use `parsim` instead of looping over `sim`:

```matlab
for k = 1:N
    in(k) = Simulink.SimulationInput('MyModel');
    in(k) = in(k).setVariable('gain', gains(k));
end
out = parsim(in);
```

`parsim` also supports `'UseFastRestart','on'` for faster batch runs.

## Guardrails

- **Never** use `set_param`, `load_system`, or `open_system` to drive simulation — `SimulationInput` replaces all of these.
- **Never** wrap `SimulationOutput` access in `try-catch` or `isfield` — `sim` either returns a valid object or throws. `SimulationOutput` has no `isfield` method.
- **Never** create unnecessary intermediate variables for logged data — access directly via `out.logsout.get('name').Values`.
- **Always** use `in`/`out` as variable names for `SimulationInput`/`SimulationOutput`.
- **Always** use `setExternalInput` with a `Dataset` — don't pass comma-separated lists of variables.

----

Copyright 2026 The MathWorks, Inc.

----

More from matlab/simulink-agentic-toolkit

SkillDescription
author-modeladvisor-checks>
authoring-simulink-inputs>
building-architecture-modelsCommon steps for building multi-layer system architecture models using System Composer. Use when implementing architecture models or when interacting with interface dictionaries, allocation sets, stereotypes, and requirements for architecture components.
building-simulink-modelsBuilds and edits Simulink, System Composer, Stateflow, and Simscape models. Use when modifying model structure, parameters, ports, connections, or Stateflow chart internals.
checking-model-complianceUse this skill when the user asks to check Simulink model compliance against a standard (MISRA, MAB, JMAAB, ISO 26262, ISO 25119, DO-178C, DO-254, IEC 61508, IEC 62304, EN 50128, CERT C/CWE, AUTOSAR), wants to run Model Advisor checks, or needs a compliance report with fix suggestions. For JMAAB/MAB, supplement deterministic checks with agentic review of uncheckable guidelines.
configuring-block-policyGuide users through creating and managing .satk/block-policy.json for controlling which blocks the agent can use, which are excluded, and which block parameters the agent should not modify. Use when setting up block usage policy for a project.
create-sdi-run>
curating-library-kgGuide users through curating the library knowledge index — reviewing block categories, marking common/important blocks, and improving block descriptions for better agent block selection.
filing-bug-reportsGenerate a standalone bug report that another developer can use to reproduce, investigate, and fix an issue. Use when the user says 'file a bug', 'write a bug report', 'report this issue', or asks to document a defect for handoff.
generate-requirement-draftsGenerates draft requirements from Simulink models. Use when drafting or updating requirement artifacts from a model. Prefers Requirements Toolbox (.slreqx) when available; falls back to structured YAML.