fopen

Open target computer file for reading and writing

Description

file_id = fopen(filesys_object, file_name) opens the file of the specified name on the target computer for reading binary data.

There are the following limitations:

  • You can have at most 128 files open on the target computer at the same time.

  • The largest single file that you can create on the target computer is 4 GB.

  • A fully qualified folder name can have a maximum of 248 characters, including the drive letter, colon, and backslash.

  • A fully qualified file name in the operating system on the target computer can have a maximum of 260 characters. If the file name is longer than eight-dot-three format (eight character file name, period, three character extension), the operating system represents the file name in truncated form (for example, six characters followed by '~1'). MATLAB commands can access the file using the fully qualified file name or the truncated representation of the name. Some block parameters, such as the Scope block filename parameter, require 8.3 format for the file name.

  • Do not write data to the private folder on your target computer. It is reserved for Simulink® Real-Time™ internal use.

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.

example

file_id = fopen(filesys_object, file_name, permission) opens the file of the specified name on the target computer for reading binary data.

Examples

collapse all

Open file data.dat with default permissions, read from it, and close it again.

Open and read file.

tg = slrt;
filesys_object = SimulinkRealTime.fileSystem(tg);
file_id = fopen(filesys_object, 'data.dat');
value = fread(filesys_object, file_id);
char(value)
ans =

  1×4 char array

test

Close file.

fclose(filesys_object, file_id)
ans =

     0

Open file data.dat, write to it, and close it again.

Open and write file.

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

Close file.

fclose(filesys_object, file_id)
ans =

     0

Open file data.dat, read from it, and close it again.

Open and read file.

tg = slrt;
filesys_object = SimulinkRealTime.fileSystem(tg);
file_id = fopen(filesys_object, 'data.dat', 'r');
value = fread(filesys_object, file_id);
char(value)
ans =

  1×4 char array

test

Close 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

The name of the file can be a name relative to the current folder or a fully qualified path. Enclose the file name in single quotation marks.

Example: 'data.dat'

The permission values have the following meaning:

  • 'r' — Open the file for reading (default). If the file does not exist, the method does not do anything.

  • 'w' — Open the file for writing. If the file does not exist, the method creates the file.

  • 'a' — Open the file for appending to it. Initially, the file pointer is at the end of the file. If the file does not exist, the method creates the file.

  • 'r+' — Open the file for reading and writing. Initially, the file pointer is at the beginning of the file. If the file does not exist, the method does not do anything.

  • 'w+' — Open the file for reading and writing. If the file exists, the method empties the file and places the file pointer at the beginning of the file. If the file does not exist, the method creates the file.

  • 'a+' — Open the file for reading and appending to the file. Initially, the file pointer is at the end of the file. If the file does not exist, the method creates the file.

Example: 'w'

Output Arguments

collapse all

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

Example: h

Introduced in R2014a