Read data from file on target computer
Simulink® Real-Time™ Library for Target Management
The From File block reads data from a file on the target computer hard disk and outputs that data in chunks every sample time. As the Simulink Real-Time kernel on the target computer reads the file data, it writes that data into a software buffer whose size is user-defined. The From File block then reads the data from this buffer and propagates it to the block outputs for use by the real-time application. For example, use the From File block to drive a model with externally acquired data (data from a file).
The From File block distributes the data as a sequence of bytes. To use these data bytes as input to a model, convert the data into one or more signals. To do so, use the Byte Unpacking block. This block outputs data in various Simulink data types. For example, assume that data in your file represents a single precision scalar and a double precision vector of width 3. To convert data of this type, set up the block to output every sample time:
28 bytes (1 * sizeof('single') + 3 * sizeof('double))
This data can then be converted into signal values by the Byte Unpacking block.
See the following topics:
Before you use a target computer file as the source for the From File block, format the data in the file. The file format is a concatenation of the different data elements for one time step, followed by the next time step, and so on.
For example, assume that your file contains the data from the preceding example. Assign a variable to each component, for example,
a — single precision value
b — double precision vector
Assume, also, that there are
N time steps
worth of data. The array dimension for
In sequence, write out the data like the following to create the file.
a(1, 1) 4 bytes b(:, 1) 24 bytes a(1, 2) 4 bytes b(:, 2) 24 bytes ... ... a(1, N) 4 bytes b(:, N) 24 bytes
If you already have the data as MATLAB® variables, use the
to create the file on the development computer. This function has
the following syntax:
SimulinkRealTime.utils.bytes2file(filename, var1, ... varn)
filename — Specify the name
of the data file from which the From File block distributes data
var1, ... varn — Specify
the column of data to be output to the model.
You can then use
download the file to the target computer.
Enter the name of the target computer file that contains the data.
Enter the size, in bytes, of the data to be distributed each sample time.
Enter the size of the software FIFO, in bytes. The Simulink Real-Time kernel fills this FIFO with the data to be input to the model. The From File block empties this FIFO as it inputs the data to the model.
This parameter should ideally be
Much larger than Output port width
At least several time the disk read size
Increasing this parameter value helps prevent the real-time application from emptying the buffer faster than the background task can fill it. This can happen if you have multitasking models or conditionally executed subsystems, which can cause temporary increases in task execution time and leave less time for the background task to fill the buffer.
Enter the number of bytes to read to fill the buffer.
To understand this parameter, assume the following default values:
Buffer size is 2000
Disk read size is 512
Output port width is 8
This means that the data buffer is of size 2000.
This buffer is initially full. Each time the block executes, eight bytes are output to the model, and the number of bytes in the buffer decreases by eight. Each time the number of free bytes in the buffer goes to 512 or higher, the Simulink Real-Time kernel attempts to read 512 bytes from the Simulink Real-Time data file to fill the buffer.
Setting this parameter to another value, for example 1024, causes the From File block to wait until 1024 bytes are free before attempting the next read.
For efficiency, set this value to a multiple of 512 (a disk sector is 512 bytes).
Select the behavior of the block for when you run the real-time application beyond when you have data in the file. Select
Hold last output — Stops
reading and stops the output at the last value
Seek to beginning — Returns
to the beginning of the file and starts reading the data (this option
results in periodic data)
Enter the base sample time or a multiple of the base sample
-1 means sample time is inherited).
Select the Show IsValid port check box
to make the port IsValid visible in the model. Port IsValid outputs
the file read succeeds and
0 if it fails.