Documentation

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

From File

Load data from MAT-file

Library

Sources

Description

The From File block loads data from a MAT-file to a model 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 data can be in array format or MATLAB® timeseries format.

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

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

The supported MAT-file versions are Version 7.0 or earlier and Version 7.3. The From File block incrementally loads data from Version 7.3 files.

You can specify how the data is loaded, including:

  • Sample time

  • How to handle data for missing data points

  • Whether to use zero-crossing detection

For more information, see Load Data Using the From File Block.

Code Generation Requirements

For a From File block, generating code that builds ERT or GRT targets or uses SIL or PIL simulation modes requires that:

  • The MAT-file contains a nonempty, finite, real matrix with at least two rows.

    • Use a data type of double for the matrix.

    • Do not include any NaN, Inf, or -Inf elements in the matrix.

  • In the From File block parameters dialog box:

    • Set the Data extrapolation before first data point and Data extrapolation after last data point parameters to Linear extrapolation.

    • Set the Data interpolation within time range parameter to Linear interpolation.

    • Clear the Enable zero-crossing detection parameter.

Data Type Support

For MATLAB timeseries data, the From File block loads real or complex signal data of any data type that Simulink® supports. Fixed-point data cannot have a word length that exceeds 32 bits. The From File block supports loading nonvirtual bus signals in timeseries format.

For array data, the From File block loads only double signal data.

For more information, see Create Data for a From File Block.

Parameters

File name

The path or file name of the MAT-file that contains the input data. Specify a path or file name in one of these ways:

  • Browse to a folder that contains a valid MAT-file.

    On UNIX® systems, the path name can start with a tilde (~) character, which means your home folder.

  • Enter the path for the file in the text box.

The default file name is untitled.mat. If you specify a file name without path information, Simulink loads the file in the current folder or on the MATLAB path. (To determine the current folder, at the MATLAB command prompt enter pwd.)

To preview the data in the Signal Preview window, click the view button .

Use the Signal Preview window to plot and inspect data.

  1. To plot the signal, select the check box next to the signal. If the format is a bus, click the expander to see and select the elements of the bus.

  2. Explore the plots by using the Measure and Zoom & Pan sections of the toolstrip.

    • In the Measure section, use the Data Cursors button to display one or two cursors for the plot. These cursors display the T and Y values of a data point in the plot. To view a data point, click a point on the line.

    • In the Zoom & Pan section, select how you want to zoom and pan the signal plots. Zooming is only for the selected axis.

      Zoom ActionButton

      Zoom in along the time and data value axes.

      Zoom in along the time axis. Click the button. On the graph, drag the cursor to select an area to enlarge.

      Zoom in along the data value axis. Click the button. On the graph, drag the cusor to select an area to enlarge.

      Zoom out from the graph.

      Fit the plot to the graph. Click the button. Click the graph to enlarge the plot to fill the graph.

      Pan the graph up, down, left, or right. Click the button. On the graph, drag the cursor to the area of the graph that you want to view.

Command-Line Information

Parameter: FileName
Type: character vector
Default: 'untitled.mat'

Output data type

The data type for the data that the From File block outputs. For nonbus 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 specified data type. For more information, see Control Signal Data Types.

  • Inherit: auto — Default

  • double

  • single

  • int8

  • uint8

  • int16

  • uint16

  • int32

  • uint32

  • boolean

  • fixdt(1,16,0) — Data type is fixed point (1,16,0).

  • fixdt(1,16,2^0,0) — Data type is fixed point (1,16,2^0,0).

  • Enum: <class_name> — Data type is enumerated.

  • Bus: <bus_object> — Data type is a bus object. For details, see the "Using Bus Data" section.

  • <data type expression> — The name of a data type object, for example Simulink.NumericType. Do not specify a bus object as the expression.

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

>> (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 Control Signal Data Types.

  • Inherit — Inheritance rule for data types. Selecting Inherit enables a second menu and text box. (Default)

  • Built in — Built-in data types. Selecting Built in enables a second menu and text box. Select one of the following choices:

    • double — Default

    • single

    • int8

    • uint8

    • int16

    • uint16

    • int32

    • uint32

    • boolean

    • Fixed point — Fixed-point data types

    • Enumerated — Enumerated data types. Selecting Enumerated enables a second menu/text box to the right, where you can enter the class name.

    • Bus — Bus object. Enables a Bus object parameter, where you enter bus object name for defining the structure of the bus. If you create or change a bus object, open the Simulink Bus Editor by clicking Edit. For details, see Create Bus Objects with the Bus Editor.

    • Expression — Expression that evaluates to a data type. Selecting Expression enables a second menu and text box to the right, where you enter the expression. Do not specify a bus object as the expression.

Sample time

The sample period and offset.

The From File block loads data from a MAT-file, using a sample time that either:

  • You specify for the From File block.

  • The From File block inherits from the blocks into which the From File block feeds data.

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

Command-Line Information

Parameter: SampleTime
Type: character vector
Default: '0'

Data extrapolation before first data point

Extrapolation method for a simulation time hit that occurs before the initial 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, the From File block:

    • Upcasts the data to double

    • Performs linear extrapolation (as described for double data)

    • 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. 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

Command-Line Information

Parameter: ExtrapolationBeforeFirstDataPoint
Type: character vector
Value: 'Linear extrapolation' | 'Hold first value' | 'Ground value'
Default: 'Linear extrapolation'

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 Boolean data, uses false for the first half of the sample and true for the second half.

  • For a built-in data type other than double or Boolean, the From File block:

    • Upcasts the data to double

    • Performs linear interpolation, as described for double data

    • 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. 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

Command-Line Information

Parameter: InterpolationWithinTimeRange
Type: character vector
'Linear interpolation' | 'Zero order hold'
Default: 'Linear interpolation'

Data extrapolation after last data point

The extrapolation method for a simulation time hit that occurs 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 first data value.

  • For built-in data types other than double or Boolean:

    • Upcasts the data to double

    • Performs linear extrapolation, as described for double data

    • 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. 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

Command-Line Information

Parameter: ExtrapolationAfterLastDataPoint
Type: character vector
'Linear extrapolation' | 'Hold last value' | 'Ground value'
Default: 'Linear extrapolation'

Enable zero-crossing detection

Enables zero-crossing detection.

The Zero-Crossing Detection parameter applies only if the Sample time parameter is set to 0 (continuous).

Simulink uses a technique known as zero-crossing detection to locate a discontinuity in time stamps, without resorting to excessively small time steps. "Zero-crossing" represents a discontinuity.

For the From File block, zero-crossing detection occurs only at time stamps in the file. Simulink examines only the time stamps, not the data values.

For bus signals, Simulink detects zero-crossings across all leaf bus elements.

If the input array contains duplicate time stamps (more than one entry with the same time stamp), Simulink detects a zero crossing at those time stamps. For example, suppose that 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 nonduplicate time stamps, zero-crossing detection depends on the settings of the following parameters:

  • Data extrapolation before first data point

  • Data interpolation within time range

  • Data extrapolation after last data point

The From File block determination of when zero-crossing occurs depends on the time stamp.

Time StampSetting

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.

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

  • Data extrapolation before first data pointLinear extrapolation

  • Data interpolation within time range (for internal points) — Zero order hold

  • Data extrapolation after last data pointLinear extrapolation

This 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):

  • Data extrapolation before first data pointHold first value

  • Data interpolation within time rangeZero order hold

  • Data extrapolation after last data pointHold last value

Examples

From File Block Loading Timeseries Data

Create a MATLAB® timeseries object with time and signal values. Save the timeseries object to a MAT-file and load into a model using a From File block.

Create an array with the time and signal data, specifying signal data for 10 time steps.

t = .1*(1:10);
d = .2*(1:10);
x = [t;d];

Create a MATLAB timeseries object.

ts = timeseries(x(2:end,:),x(1,:))
  timeseries

  Common Properties:
            Name: 'unnamed'
            Time: [10x1 double]
        TimeInfo: tsdata.timemetadata
            Data: [1x1x10 double]
        DataInfo: tsdata.datametadata

Save the timeseries object in a Version 7.3 MAT-file.

save('mySignals','ts','-v7.3')

Add a From File block and set the File name parameter of that block to mySignals.mat.

Simulate the model. The Scope block reflects the data loaded from the mySignals.mat file.

Characteristics

Data Types

Double | Single | Boolean | Base Integer | Fixed Point | Enumerated | Bus

Sample Time

Specified in the Sample time parameter

Multidimensional Signals

Yes

Variable-Size Signals

No

Zero-Crossing Detection

Yes, if enabled

Code Generation

Yes

Introduced before R2006a

Was this topic helpful?