Wednesday, August 5, 2009

fopen

Open a file or obtain information about open files

Syntax

  • fid = fopen(filename)
    fid = fopen(filename,permission)
    [fid,message] = fopen(filename,permission,machineformat)
    fids = fopen('all')
    [filename,permission, machineormat] = fopen(fid)

Description

fid = fopen(filename) opens the file filename for read access. (On PCs, fopen opens files for binary read access.)

fid is a scalar MATLAB integer, called a file identifier. You use the fid as the first argument to other file input/output routines. If fopen cannot open the file, it returns -1. Two file identifiers are automatically available and need not be opened. They are fid=1 (standard output) and fid=2 (standard error).

fid = fopen(filename,permission) opens the file filename in the mode specified by permission. permission can be:


'r'
Open file for reading (default).
'w'
Open file, or create new file, for writing; discard existing contents, if any.
'a'
Open file, or create new file, for writing; append data to the end of the file.
'r+'
Open file for reading and writing.
'w+'
Open file, or create a new file, for reading and writing; discard existing contents, if any.
'a+'
Open file, or create new file, for reading and writing; append data to the end of the file.
'A'
Append without automatic flushing; used with tape drives
'W'
Write without automatic flushing; used with tape drives

filename can be a MATLABPATH relative partial pathname if the file is opened for reading only. A relative path is always searched for first with respect to the current directory. If it is not found and reading only is specified or implied then fopen does an additional search of the MATLABPATH

Files can be opened in binary mode (the default) or in text mode. In binary mode, no characters are singled out for special treatment. In text mode on the PC, , the carriage return character preceding a newline character is deleted on input and added before the newline character on output. To open in text mode, add "t" to the permission string, for example 'rt' and 'wt+'. (On Unix, text and binary mode are the same so this has no effect. But on PC systems this is critical.)

    Note If the file is opened in update mode ('+'), an input command like fread, fscanf, fgets, or fgetl cannot be immediately followed by an output command like fwrite or fprintf without an intervening fseek or frewind. The reverse is also true. Namely, an output command like fwrite or fprintf cannot be immediately followed by an input command like fread, fscanf, fgets, or fgetl without an intervening fseek or frewind.

[fid,message] = fopen(filename,permission) opens a file as above. If it cannot open the file, fid equals -1 and message contains a system-dependent error message. If fopen successfully opens a file, the value of message is empty.

[fid,message] = fopen(filename,permission,machineformat) opens the specified file with the specified permission and treats data read using fread or data written using fwrite as having a format given by machineformat. machineformat is one of the following strings:


'cray' or 'c'

Cray floating point with big-endian byte ordering

'ieee-be' or 'b'

IEEE floating point with big-endian byte ordering

'ieee-le' or 'l'

IEEE floating point with little-endian byte ordering

'ieee-be.l64' or 's'

IEEE floating point with big-endian byte ordering and 64-bit long data type

'ieee-le.l64' or 'a'

IEEE floating point with little-endian byte ordering and 64-bit long data type

'native' or 'n'

Numeric format of the machine on which MATLAB is running (the default).

'vaxd' or 'd'

VAX D floating point and VAX ordering

'vaxg' or 'g'

VAX G floating point and VAX ordering

fids = fopen('all') returns a row vector containing the file identifiers of all open files, not including 1 and 2 (standard output and standard error). The number of elements in the vector is equal to the number of open files.

[filename,permission,machineformat] = fopen(fid) returns the filename, permission string, and machineformat string associated with the specified file. An invalid fid returns empty strings for all output arguments.

The 'W' and 'A' permissions are designed for use with tape drives and do not automatically perform a flush of the current output buffer after output operations. For example, open a 1/4" cartridge tape on a SPARCstation for writing with no auto-flush:

  •            fid = fopen('/dev/rst0','W')

Examples

The example uses fopen to open a file and then passes the fid, returned by fopen, to other file I/O functions to read data from the file and then close the file.

  • fid=fopen('fgetl.m');
    while 1
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    disp(tline)
    end
    fclose(fid);



fopen (serial)



Connect a serial port object to the device

Syntax

  • fopen(obj)

Arguments


obj
A serial port object or an array of serial port objects.

Description

fopen(obj) connects obj to the device.

Remarks

Before you can perform a read or write operation, obj must be connected to the device with the fopen function. When obj is connected to the device:

An error is returned if you attempt to perform a read or write operation while obj is not connected to the device. You can connect only one serial port object to a given device.

Some properties are read-only while the serial port object is open (connected), and must be configured before using fopen. Examples include InputBufferSize and OutputBufferSize. Refer to the property reference pages to determine which properties have this constraint.

The values for some properties are verified only after obj is connected to the device. If any of these properties are incorrectly configured, then an error is returned when fopen is issued and obj is not connected to the device. Properties of this type include BaudRate, and are associated with device settings.

If you use the help command to display help for fopen, then you need to supply the pathname shown below.

  • help serial/fopen

Example

This example creates the serial port object s, connects s to the device using fopen, writes and reads text data, and then disconnects s from the device.

  • s = serial('COM1');
    fopen(s)
    fprintf(s,'*IDN?')
    idn = fscanf(s);
    fclose(s)

No comments:

Post a Comment