| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
Sinks
The To File block inputs a scalar or vector signal of type double and writes the signal data to a MAT-file. The block's icon shows the name of the output file. If the output file exists at the time the simulation starts, the block overwrites the file. The block writes to the output file incrementally, so memory overhead during simulation is low. The file automatically closes when simulation is complete.
Note If simulation terminates abnormally, the To File block may have written a MAT-file that contains some data, but the file is unusable. |
The To File block writes data as a matrix of two or more rows. The block writes one column to the MAT-file for each time step. The first element of the column contains the simulation time. The remainder of the column contains scalar or vector data for the time shown at the top of the column. The stored matrix has this form:

To avoid the overhead of compressing data in real time, the To File block writes an uncompressed MAT-file. To compress the file, load and save it in MATLAB. The resaved file will be smaller because MATLAB software automatically compresses MAT-files.
The MAT-file can contain at most 108 elements, where each timestamp and each data value counts as one element. Thus if N is the number of time steps in the file, and W is the width of the data, N(W+1) cannot exceed 108. If writing the next column to the file would cause the number of elements to exceed 108, the To File block discards the unwritten column, closes the file, and posts a warning. Simulation continues without interruption, but the To File block discards all subsequent data. The truncated MAT-file is syntactically valid and can be used as input to a subsequent simulation, or loaded into MATLAB for any purpose.
For a fixed-step solver, the block's Decimation and Sample Time parameters control when data is written to the file. For a variable-step solver, the Configuration Parameters > Data Import/Export Pane > Output options parameter controls the amount of data available to the To File block, and the block's Decimation and Sample Time parameters control how much of this data the block actually writes.
For example, if you need to ensure that data is written at identical time points over multiple simulations with a varying-step solver, set Output Options to Produce specified output only and enter the desired time vector in the Output times field. See Importing Data from a Workspace for guidelines on choosing time vectors for discrete systems.
The From File block can use data written by a To File block without any modifications to the data or other special provisions.
The From Workspace block requires data that is the transposition of the data written by the To File block. To provide the required format, use MATLAB commands to load and transpose the data from MAT-file. You will then be able to use a From Workspace block to access the data.
When a To File block exists in a referenced model, that model must be a single-instance model. Only one instance of such a model can exist in a model hierarchy. Otherwise, the corresponding To File blocks in different instances of the referenced model would all try to write to the same data file, which could not provide a useful result. See General Reusability Limitations for more information.
The To File block accepts only vector and scalar data, and all data must be of type double. The To File block does not accept matrix signals or complex data. To save other kinds of data, use a To Workspace block to save the data to the MATLAB workspace. You can then save the data to a file.

The pathname or filename of the MAT-file in which to store the output. On UNIX systems, the pathname can start with a tilde (~) character signifying your home directory. The default filename is untitled.mat. If you specify a filename without path information, Simulink software stores the file in the MATLAB working directory. (To determine the working directory, type pwd at the MATLAB command line.) If the file already exists, Simulink software overwrites it.
The name of the matrix contained in the named file. The default name is ans.
Specifies writing data at every nth sample, where n is the decimation factor. The default decimation is 1, which writes data at every time step.
Specifies the sample period and offset at which to collect points. This parameter is useful when you are using a variable-step solver where the interval between time steps might not be constant. The default is-1, which inherits the sample time from the driving block when determining the points to write. See How to Specify the Sample Time for more information.
Sample Time | Specified in the Sample time parameter |
Dimensionalized | Yes |
Importing and Exporting Data, From File, From Workspace, To Workspace
![]() | Time-Based Linearization | To Workspace | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |