fwrite

Write binary data to open target computer file

Description

example

fwrite(filesys_object, file_id, data) writes the elements of matrix data to the file identified by file_id. The file_id argument is the file identifier associated with an open file. fwrite requires that the file is open with write permission.

Note: The SimulinkRealTime.fileSystem object will be removed in a future release. See the release note for file system commands to use instead. These commands use the SimulinkRealTime.openFTP function and the functions for the MATLAB ftp object.

Examples

collapse all

Open magic.dat for writing, write it, close it, and read it back.

Open magic.dat for writing.

tg = slrt;
filesys_object = SimulinkRealTime.fileSystem(tg);
file_id = fopen(filesys_object, 'magic.dat', 'w');

Create and write a magic square.

msquare = magic(5)
fwrite(filesys_object, file_id, msquare);
msquare =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

Close the file.

fclose(filesys_object, file_id)
ans =

     0

Reopen the file for reading and read it.

file_id = fopen(filesys_object, 'magic.dat', 'r');
value = fread(filesys_object, file_id)
value =

  1×25 uint8 row vector

  Columns 1 through 10

   17   23    4   10   11   24    5    6   12   18

  Columns 11 through 20

    1    7   13   19   25    8   14   20   21    2

  Columns 21 through 25

   15   16   22    3    9

Close the file.

fclose(filesys_object, file_id)
ans =

     0

Input Arguments

collapse all

File system object created by using the SimulinkRealTime.fileSystem creation function.

The file system object represents the target computer file system. You work with the target computer file system from the development computer by using file system methods.

Example: fsys

Data Types: struct

Pass this value to functions that access files on the target computer.

Example: h

The data is written to the file in column order.

Example: 'test'

Introduced in R2014a