Write data to file
The To File block icon shows the name of the output file.
The block writes to the output file incrementally, with minimal memory overhead during simulation. If the output file exists when the simulation starts, the block overwrites the file. The file automatically closes when simulation is complete. If simulation terminates abnormally, the To File block saves the data it has logged up until the point of the abnormal termination.
Use the Save format parameter to specify which of the following two formats the To File block uses for writing data:
Use the Array format only for vector, double, noncomplex inputs. To save bus data, use the Timeseries format.
For the Timeseries format, the To File block:
Writes data in a MATLAB® timeseries object
Supports writing multidimensional, real, or complex output values
Supports writing output values that have any built-in data type, including Boolean, enumerated (enum), and fixed-point data with a word length of up to 32 bits
For bus input signals, creates a MATLAB structure that matches the bus hierarchy. Each leaf of the structure is a MATLAB timeseries object.
For the Array format, the To File block:
Writes data into a matrix containing two or more rows. The matrix has the following form:
Simulink® writes one column to the matrix for each data sample. The first element of the column contains the time stamp. The remainder of the column contains data for the corresponding output values.
Supports writing data that is one-dimensional, double, and noncomplex.
The To File block Decimation and Sample Time parameters control when data is written to the file.
The From File block can use data written by a To File block in any format (Timeseries or Array) without any modifications to the data or other special provisions.
The From Workspace block can read data that is in the Array format and 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 a MAT-file.
If you pause using the Simulation Stepper, the To File block captures the simulation data up to the point of the pause. When you step back, the To File data file no longer contains any simulation data past the new reduced time of the last output.
When a To File block is 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. See General Reusability Limitations for more information.
To avoid the overhead of compressing data in real time, the To File block writes an uncompressed Version 7.3 MAT-file. To compress the data within the MAT-file, load and save the file in MATLAB. The resaved file is smaller than the original MAT-file that the To File block created, because the Save command compresses the data in the MAT-file.
The To File block supports virtual and nonvirtual bus input.
To save bus data, set the Save format parameter to Timeseries.
If the input signal is a bus, then the To File block creates a MATLAB structure that matches the bus hierarchy. Each leaf of the structure is a MATLAB timeseries object.
After pausing a simulation, do not alter any file that a To File block logs into. For example, do not save such a file with the MATLAB save command. Altering the file can cause an error when you resume the simulation. If you want to alter the file after pausing, copy the file and work with the copied version of the file.
The To File block accepts real or complex signal data of any data type that Simulink supports, with the exception that the word length for fixed-point data must be 32 bits or less.
The To File block accepts bus data.
The path or file name of the MAT-file in which to store the output. On UNIX® systems, the pathname can start with a tilde (~) character signifying your home folder. The default file name is untitled.mat. If you specify a file name without path information, Simulink stores the file in the MATLAB working folder. (To determine the working folder, type pwd at the MATLAB command line.) If the file already exists, Simulink overwrites it.
The name of the matrix contained in the named file. The default name is ans.
The data format (timeseries or array) that the To File block uses for writing data. The default format is Timeseries.
The decimation factor, n, where n specifies writing data at every nth time that the block executes. 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. See Specify Sample Time for more information.