Skip to Main Content Skip to Search
Product Documentation

From File - Read data from MAT-file

Library

Sources

Description

The From File block reads data from a MAT-file and outputs the data as a signal. The data is a sequence of samples. Each sample consists of a time stamp and an associated data value.

The From File block icon shows the name of the MAT-file that supplies the data.

You can have multiple From File blocks that read from the same MAT-file.

During the current simulation, the From File block cannot read data from a MAT-file that is being written to by a To File block.

Version 7.3 MAT-Files

For a Version 7.3 MAT-file, the From File block incrementally reads data from the MAT-file during simulation. The Sample time parameter specifies the sample time that the From File block uses to read data from a MAT-file. For details, see "Parameters and Dialog Box".

The time stamps in the file must be monotonically nondecreasing.

For each simulation time hit for which the MAT-file contains no matching time stamp, Simulink software interpolates or extrapolates to obtain the needed data using the method that you select. For details, see "Simulation Time Hits That Have No Corresponding MAT-File Time Stamps."

When the From File block reads data from a MAT-file, that data must be stored in one of two formats:

Use array format only for vector, double, noncomplex signals. To load a bus signal, use a MATLAB structure that matches the bus hierarchy, where each leaf of the structure is a MATLAB timeseries object.

MATLAB timeseries format data can have:

For the array format, the stored data is a matrix containing two or more rows. The matrix in the MAT-file must have the following form:

The first element of each column contains a time stamp. The remainder of the column contains data for the corresponding output values.

For data stored using the array format, the width of the From File output depends on the number of rows in the matrix. Given a matrix containing m rows, the block outputs a vector of length m–1.

Version 7.0 and Earlier Version MAT-Files

For Version 7.0 or an earlier version of a MAT-file, the From File block reads only array-format data. The complete, uncompressed data from a MAT-file loads into memory at the start of the simulation.

If you have a Version 7.0 or earlier version MAT-file that you want to use with the From File block, consider converting the file to a Version 7.3 MAT-file. Use a Version 7.3 MAT-file if you want the From File block to incrementally read the data during simulation or you want to use MATLAB timeseries data. For example, to convert a Version 7.0 file named my_data_file.mat that contains the variable var, at the MATLAB command line, enter:

load('my_data_file.mat')
save('my_data_file.mat', 'var', '-v7.3')

Simulation Time Hits That Have No Corresponding MAT-File Time Stamps

If the simulation time hit does not have a corresponding MAT-file time stamp, then the From File block output depends on:

For details about interpolation and extrapolation options, see the descriptions of the following parameters in "Parameters and Dialog Box":

Sometimes the MAT-file includes duplicate time stamps (two or more data values that have the same time stamp). In such cases, the From File block action depends on the location of the simulation time hit, relative to the duplicate time stamps in the MAT-file.

For example, suppose the MAT-file contains the following data, with three data points having a time stamp value of 2:

time stamps:    0 1 2 2 2 3 4
data values:    2 3 6 4 9 1 5

The following table describes the From File block output.

Simulation Time, Relative to Duplicate Time Stamp Values in MAT-FileFrom File Block Action
Before the duplicate time stamps

Performs the same actions as when the time stamps are distinct, using the first of the duplicate time stamp values as the basis for interpolation. (In this example, that time stamp value is 6.)

At or after the duplicate time stamps

Performs the same actions as when the times stamps are distinct, using the last of the duplicate time stamp values as the basis for interpolation. (In this example, that time stamp value is 9.)

Using Data Saved by a To File Block

The From File block reads data written by a To File block without any modifications to the data or other special provisions.

Using Bus Data

The From File block supports loading nonvirtual bus signals.

The data must be in a MATLAB structure that matches the bus hierarchy. Each leaf of the structure must be a MATLAB timeseries object.

The data can underspecify the bus signal, but cannot overspecify the bus signal. The structure cannot have any elements that do not have corresponding signals in the bus.

The structure does not require data for every element in the bus hierarchy, but the structure must have data for at least one of the signals in the bus. For signals that do not specify data, the From File block outputs the ground values. If any data read by the From File block has variable dimensions, then you must specify data for all complex bus signals.

Data Type Support

The From File block can read real or complex signal data of any data type that Simulink supports, except that fixed-point data cannot have a word length that exceeds 32 bits.

The From File block supports reading nonvirtual bus signals.

Parameters and Dialog Box

File name

The path or file name of the MAT-file that contains the data used as input. On UNIX® systems, the path name 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 reads the file in the MATLAB working folder. (To determine the working folder, type pwd at the MATLAB command line.) If Simulink cannot find the specified file name in the working folder, it displays an error message.

Output data type

The data type for the data that the From File block loads. For non-bus types, you can use Inherit: auto to skip any data type verification. If you specify an output data type, then the From File block verifies that the data in the file matches the data type that you specified. For more information, see Specifying Block Output Data Types.

If you set Output data type to be a bus object, the bus object must be available when you compile the model. For each signal in a bus, the From File block verifies that the data type, dimension, and complexity of the data matches the data type, dimension, and complexity that the bus object defines for the signal.

>> (Show data type assistant)

Displays the Data Type Assistant, to help you to set the Output data type parameter.

Mode

The category of data to specify. For more information, see Specifying Block Output Data Types.

Sample time

The sample period and offset of the data read from the file.

The From File block reads data from a MAT-file using a sample time that:

The default is 0, which specifies a continuous sample time, The MAT-file is read at the base (fastest) rate of the model. For details, see How to Specify the Sample Time.

Data extrapolation before first data point

Extrapolation method that Simulink uses for a simulation time hit that is before the first time stamp in the MAT-file. Choose one of the following extrapolation methods.

MethodDescription
Linear extrapolation

(Default)

If the MAT-file contains only one sample, then the From File block outputs the corresponding data value.

If the MAT-file contains more than one sample, then the From File block linearly extrapolates using the first two samples:

  • For double data, linearly extrapolates the value using the first two samples

  • For Boolean data, outputs the first data value

  • For a built-in data type other than double or Boolean, upcasts the data to double, performs linear extrapolation (as described above for double data), and then downcasts the extrapolated data value to the original data type

You cannot use the Linear extrapolation option with enumerated (enum) data. All signals in a bus use the same extrapolation setting, so if any signal in a bus uses enum data, then you cannot use the Linear extrapolation option.

Hold first value

Uses the first data value in the file

Ground value

Uses a value that depends on the data type of MAT-file sample data values:

  • Fixed-point data types — uses the ground value

  • Numeric types other than fixed–point — uses 0

  • Boolean — uses false

  • Enumerated data types — uses default value

Data interpolation within time range

The interpolation method that Simulink uses for a simulation time hit between two time stamps in the MAT-file. Choose one of the following interpolation methods.

MethodDescription
Linear interpolation

(Default)

The From File block interpolates using the two corresponding MAT-file samples:

  • For double data, linearly interpolates the value using the two corresponding samples

  • For a built-in data type other than double or Boolean, upcasts the data to double, performs linear interpolation (as described above for double data), and then downcasts the interpolated value to the original data type

You cannot use the Linear interpolation option with enumerated (enum) data. All signals in a bus use the same interpolation setting, so if any signal in a bus uses enum data, then you cannot use the Linear interpolation option.

Zero order hold

Uses the data from the first of the two samples

Data extrapolation after last data point

The extrapolation method that Simulink uses for a simulation time hit that is after the last time stamp in the MAT-file. Choose one of the following extrapolation methods.

MethodDescription
Linear extrapolation

(Default)

If the MAT-file contains only one sample, then the From File block outputs the corresponding data value.

If the MAT-file contains more than one sample, then the From File block linearly extrapolates using data values of the last two samples:

  • For double data, extrapolates the value using the last two samples

  • For Boolean data, outputs the last data value

  • For a built-in data type other than double or Boolean, upcasts the data to double, performs linear extrapolation (as described above for double data), and then downcasts the extrapolated value to the original data type

You cannot use the Linear extrapolation option with enumerated (enum) data. All signals in a bus use the same extrapolation setting, so if any signal in a bus uses enum data, then you cannot use the Linear extrapolation option.

Hold last value

Uses the last data value in the file

Ground value

Uses a value that depends on the data type of MAT-file sample data values:

  • Fixed-point data types — uses the ground value

  • Numeric types other than fixed–point — uses 0

  • Boolean — uses false

  • Enumerated data types — uses default value

Enable zero-crossing detection

Select to enable zero-crossing detection.

For details, see Zero-Crossing Detection, as well as the discussion below.

Simulink uses a technique known as zero-crossing detection to accurately locate a discontinuity, without resorting to excessively small time steps. This section uses "zero-crossing" to represent discontinuities.

For the From File block, zero-crossing detection can only occur at sample points in the file. To determine whether zero crossing occurs, the From File block examines only the time stamps within the sample points.

If the input array contains duplicate time stamps (more than one entry for the same time stamp), Simulink detects a zero crossing at that time stamp. For example, suppose the input array has this data:

time:     0 1 2 2 3
signal:   2 3 4 5 6

At time 2, there is a zero crossing from the input signal discontinuity.

For data with nonduplicate time stamps, zero-crossing detection depends on the settings of the following parameters:

The From File block applies the following rules when determining when zero-crossing occurs for the first time stamp, for time stamps between the first and last time stamp, and for the last time stamp.

Time StampWhen Zero-Crossing Detection Occurs

First

Data extrapolation before first data point is set to Ground value.

Between first and last

Data interpolation within time range is set to Zero-order hold.

Last

One or both of these settings occur:

  • Data extrapolation after last data point is set to Ground value.

  • Data interpolation within time range is set to Zero-order hold.

The following figure illustrates zero-crossing detection for data accessed by a From File block that has the following settings:

The following figure is another illustration of zero-crossing detection for data accessed by a From File block. The block has the following settings for the time stamps (points):

Characteristics

Sample Time

Specified in the Sample time parameter

Scalar Expansion

No

Dimensionalized

Yes

Zero Crossing

Yes, if enabled

See Also

Importing and Exporting Simulation Data, From Workspace, To File, To Workspace

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS