> 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/rt-lab/simulink-blocks-reference/rt-lab-library/opwritefile.md).

# OpWriteFile

## Description

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-45155646e5594b9c43fe7a5ac0b08b75d1c7404f%2FRTL_opwrite_file_block.png?alt=media" alt=""><figcaption></figcaption></figure>

The **OpWriteFile** block, located in the **RT-LAB/DataLogging** library, writes the simulation time and input signal(s) to a MATLAB MAT binary file on the simulator, one column per time step. The first row contains the simulation time (automatically added); subsequent rows contain the elements of the input vector signal.

Recording starts when the simulation begins. You can control when data is written by using the **OpTrigger** block; the acquisition group must match in both blocks.

* After a trigger occurs and recording completes (File size limit reached), the MATLAB file is not overwritten by subsequent triggers.
* Clear the static filename option to record multiple files during the same simulation.
* After logging completes or the model is reset, the file is transferred to the workstation.

If Buffer size or File size limit are not set correctly, the resulting file may be empty.

{% hint style="info" %}
Buffer size (bytes) >= (Number of logged signals + 1) × (Number of samples per signal) × 8

* The time signal is automatically added as the first row (+1).
* Double precision uses 8 bytes per sample.
* A typical starting value for Number of samples per signal is 1000.

File size (bytes) = 1.5 × (Number of logged signals + 1) × 8 × (Logging duration (s)) / (Decimation factor × Sample time (s))

* The 1.5 factor accounts for file header overhead and may be adjusted as needed.
* Double precision uses 8 bytes per sample.
* Sample time is the simulation step (e.g., 25 or 50 µs).
* Logging duration is user-defined.
  {% endhint %}

For a Linux-based simulator using a relative path, the file is transferred to the OpREDHAWKtarget folder of the corresponding subsystem. For example, if the model is named rtdemo1 and the OpWriteFile block is in the sm\_computation subsystem, the file is transferred to: \models\rtdemo1\rtdemo1\_sm\_computation\OpREDHAWKtarget\ .mat

## Mask parameters

<figure><img src="https://96827845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNRdpSk8kjn20gXAexnt6%2Fuploads%2Fgit-blob-934e27f189014e7e4b4fb59a2e1dee06f0c034e0%2FRTL_opwrite_file_mask.png?alt=media" alt="" width="407"><figcaption></figcaption></figure>

<table><thead><tr><th width="249.79998779296875">Parameter</th><th>Description</th></tr></thead><tbody><tr><td><strong>Static filename</strong></td><td><p>When checked (default), the created file uses the name in Filename. Once full, it is not overwritten for the rest of the simulation. When unchecked, a File ID input is added. Its value is appended to the filename. Each time the File ID value changes, the block closes the current file (even if not full) and opens a new file with the new suffix.<br>Example assumptions:</p><ul><li>Filename is 'myfile.mat'</li><li>Static filename is unchecked</li><li>A constant with value 12 is connected to the File ID input</li><li>No OpTrigger block</li></ul><p>At simulation start, the block writes to 'myfile_12.mat'. If the constant changes from 12 to 15, 'myfile_12.mat' is closed and 'myfile_15.mat' is opened. If an OpTrigger block is present and the File ID changes, the previous file closes, and you must trigger again to start recording in the new file.<br><br>Alternatively, filenames can be defined in a CSV file. Set Filename to: ".csv, &#x3C;column index starting from 0>". The File ID input then selects the row (starting at 0).</p><p>For example, if Filename is "csvfile.csv, 1" (quotes required), the File ID is 1, and the CSV content is:</p><ul><li>out_folder/out_file_a.mat</li><li>out_folder/out_file_b.mat</li><li>alternative_out_folder/out_file_c.mat</li><li>then the filename used is "out_file_b.mat".</li></ul></td></tr><tr><td><strong>Decimation factor</strong></td><td>Decimation (sampling) factor. For example, 5 saves one sample every five steps. Default: 1.</td></tr><tr><td><strong>Variable name</strong></td><td>Name of the matrix saved in the file. This is the variable name visible in MATLAB when opening the file.</td></tr><tr><td><strong>Filename</strong></td><td>Directory and file name for the data. The path can be relative or absolute. If it does not exist, it is created.</td></tr><tr><td><strong>Real-Time Path</strong></td><td>Location of the file when the simulation runs on the simulator node. Default: './'.</td></tr><tr><td><strong>Non-Real Time Path</strong></td><td>Location of the file when the model runs on the workstation (Windows). Default: '.'.</td></tr><tr><td><strong>Acquisition group</strong></td><td>Acquisition group for all signals through this block. Options: 26 to 30.</td></tr><tr><td><strong>File size limit</strong></td><td>When this limit is reached, the file is closed.</td></tr><tr><td><strong>Write in Simulink mode</strong></td><td>When checked, writes a file when the simulation runs in Simulink. The MAT file is saved beside the model (.mdl or .slx). Default: unchecked.</td></tr><tr><td><strong>Simulation mode</strong></td><td><p>Leave unchecked (default) to use RT-LAB data acquisition without affecting real-time execution. When checked, the block runs in simulation without data loss mode: all samples are written, and the model waits for file writes before the next step. Real-time is not maintained. This option ignores:</p><ul><li>Trigger block (if any) for the acquisition group</li><li>Option "Number of samples"</li><li>Option "Buffer size in bytes"</li></ul></td></tr><tr><td><strong>Number of samples</strong></td><td>Samples per signal written at a time. For example, 1000 guarantees no data loss within each group of 1000 samples. Gaps can occur only between groups. If using an OpTrigger block with the same acquisition group, the default (1000) is recommended for efficiency.</td></tr><tr><td><strong>Buffer size in bytes</strong></td><td>Size of the internal circular buffer (in bytes) used to accumulate Number of samples before writing to file. Each sample is 8 bytes. Only complete buffers are written; if the buffer is too large to fill, the file may remain empty.</td></tr></tbody></table>

## Inputs

<table><thead><tr><th width="250.5999755859375">input</th><th>Description</th></tr></thead><tbody><tr><td><strong>Data</strong></td><td>Input signal vector.</td></tr><tr><td><strong>File id</strong></td><td>Value appended to the filename. Available only when Static filename is unchecked.</td></tr></tbody></table>

## Outputs

<table><thead><tr><th width="249.79998779296875">Output</th><th>Description</th></tr></thead><tbody><tr><td><strong>File Limit Reached</strong></td><td>Set to 1 when the file size limit is reached.</td></tr><tr><td><strong>File status</strong></td><td>Set to 1 when the file is open, 0 when closed, and -1 while opening or closing. In Simulink mode, this output is always 1.</td></tr></tbody></table>

## Characteristics and limitations

| Characteristic       | Value                  |
| -------------------- | ---------------------- |
| Direct Feedthrough   | Yes                    |
| Discrete sample time | No                     |
| XHP support          | Yes (Partial XHP only) |
| Work offline         | Yes                    |


---

# 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/rt-lab/simulink-blocks-reference/rt-lab-library/opwritefile.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.
