> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/hypersim/model-and-simulation/import-models-from-external-software-or-code/import-from-simulink-hyperlink/export-from-simulink/generate-code-for-multiple-time-step.md).

# Generate Code for Multiple Time-Step

This page explains how to generate code for multiple time steps to allow a Simulink model to run in HYPERSIM over a predefined range of simulation time steps, without regenerating code for each time step. This feature is particularly useful when testing or deploying the same model at different HYPERSIM time steps, for example to meet real‑time constraints or to evaluate performance.

## Requirements

* Supported MATLAB versions: **R2015b up to R2019b**
* Requires **Simulink Coder** (**Embedded Coder not supported**)
* **Not compatible with protected (confidential) Simulink models**, such as models compiled for IP protection (e.g., `.slxp`), models with code protection enabled, or models where the internal structure is hidden and only the interface (inputs/outputs) is accessible.
* **Protected S-Functions are not supported** with multiple time-step code generation

## Procedure

{% stepper %}
{% step %}

### Set the Simulink model time step to a variable

In the model configuration, set the **Fixed-step size** to a variable (e.g. `Ts`).

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-79f205f493109395f306faada4c2e75277dc0620%2Fsimulink-import-time-step.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Define the variable in MATLAB workspace

For example, enter the following command in the MATLAB console before generating the code:

```matlab
Ts = 50e-6; % 50 µs
```

{% endstep %}

{% step %}

### Enable multiple time-step code generation

Open Simulink **Code Generation** options and enable **Simulink Model is a multi-time step compatible**. Then, specify:

* Minimum time step
* Maximum time step
* Time step increment

These values define the range of supported time steps. Choose a range that matches the HYPERSIM time steps you expect to use. Note that generating code for a larger number of time steps increases generation time and complexity.

<figure><img src="https://3623212549-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLxgTpoh7kyPmQ5vuyqD7%2Fuploads%2Fgit-blob-59ef37d31f735230d73bee323d970932fa4e1184%2Fsimulink-import-time-step-compatible.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Set the HYPERSIM environment variable

In the MATLAB console, set the `HYPERSIM_DIR` environment variable to your HYPERSIM installation directory:

```matlab
setenv('HYPERSIM_DIR','C:\OPAL-RT\HYPERSIM\hypersim_%version%')
```

Replace `%version%` with your installed HYPERSIM version.
{% endstep %}

{% step %}

### Generate code and import into HYPERSIM

Use the standard workflow to generate the code from Simulink, then import the generated `.def` file into HYPERSIM. Althought only the `ucm_%model_name%.def` file is selected during the import process in HYPERSIM, **all files in the generated folder are required** for a successful import.
{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/hypersim/model-and-simulation/import-models-from-external-software-or-code/import-from-simulink-hyperlink/export-from-simulink/generate-code-for-multiple-time-step.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
