<< >> Title Contents Index


MathViews Functions


Function Reference

fclose - close file(s)

Synopsis:

fclose(fHandle)

Description:

The fclose function closes the file(s) associated with fHandle.

Example:

A=1:10
fid = fopen('temp1.txt', 'w+')
nCnt = fwrite(fid, A, 'ushort')
fseek(fid,0,'b')
[B, numpoints] = fread(fid,2,'ushort')
currentpos = ftell(fid)
C = fread(fid,2,'ushort')
currentpos = ftell(fid)
fclose(fid)

Related Topics:


File I/O functions

feof - check for EOF (end of file)

Synopsis:

feof(fHandle)

Description:

The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file associated with fHandle. It returns 0 if the current position is not end of file. There is no error return

The feof routine determines whether the end of file has been reached. When end of file is reached, read operations return an end-of-file indicator until the file is closed or until rewind, ftell, or fseek is called.

Example:

Related Topics:


File I/O functions

ferror - check for file i/o errors

Synopsis:

ferror(fHandle)

Description:

The ferror tests for a reading or writing error on the file associated with fHandle. If an error has occurred, the error indicator for the file remains set until the file is closed or rewind is called.

Example:

Related Topics:


File I/O functions

fgetl - get line from file (ignore CR)

Synopsis:

string = fgetl(fHandle)

Description:

The fgetl returns a string from the the file associated with fHandle. It reads characters from the current file position to and including the first newline character or to the end of the file whichever comes first. The newline character, if read, is not included in the string (it is discarded).

Example:

Related Topics:


File I/O functions

fgets - get line from file

Synopsis:

string = fgets(fHandle)

Description:

The fgets returns a string from the file associated with fHandle. It reads characters from the current file position to and including the first newline character or to the end of the file whichever comes first. The newline character, if read, is included in the string.

Example:

Related Topics:


File I/O functions

fopen - open file(s)

Synopsis:

[fHandle, errmsg]=fopen(filename)

[fHandle, errmsg]=fopen(filename, mode)

fHandle=fopen('all')

Description:

The fopen function opens the file specified by filename and returns a handle, fHandle, to the file. fHandle is an index to an internally maintain array of open file handles. fHandle = -1 is an invalid handle, the string errmsg provides more information regarding the error.

fopen('all') returns the array of open file handles. You can NOT open a filename 'all'.

The fHandle=fopen(filename, mode) function opens the file specified by filename. The character string mode specifies the type of access requested for the file, as follows:

fopen modes
'r'
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
'w'
Opens an empty file for writing. If the given file exists, its contents are destroyed.
'a'
Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist. This mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file.
'r+'
Opens existing file for both reading and writing. (The file must exist).
'w+'
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
'a+'
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

Example:

Related Topics:


File I/O functions

fprintf - write data to file using C-style output formating

Synopsis:

byteCount=fprintf(fHandle, formatString, arg1,...)

Description:

The fprintf formats the data of arg1 (and any additional variable listed after arg1) and writes a series of characters/values to the file associated with fHandle. Each function argument (if any) is converted and output according to the corresponding format specification in format.

formatString specifications always begin with a percent sign (%) and are read left to right. When printf encounters the first format specification (if any), it converts the value of the first argument (if the argument is a matrix, all the elements are converted) after format and outputs it accordingly. The second format specification causes the second argument to be converted and output, and so on. If there are more arguments than there are format specifications, the extra arguments are ignored. Refer to your C language reference manual for additional information regarding escape characters and format specifications.

Format specification
consists of optional and required fields, has the following form: %[flags] [width] [.precision]type
Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%. The optional fields, which appear before the type character, control other aspects of the formatting
type
e
Signed value having the form [ – ]d.dddd e [sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or –.
E
Identical to the e format except that E rather than e introduces the exponent.
f
Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
g
Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
G
Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate).
s
Character string printed for the values of the argument. Matrix rows are converted to a character string which are printed up to the first null character (value 0) or until the precision value is reached.
flags
The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.
-
Left align the result within the given field width
+
Prefix the output value with a sign (+ or –) if the output value is of a signed type
0
If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and – appear, the 0 is ignored.
#
When used with the e, E, or f format, the # flag forces the output value to contain a decimal point in all cases.
When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.
width
The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers). The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).
precision
The third optional field of the format specification is the precision specification. It specifies a nonnegative decimal integer, preceded by a period (.), which specifies the number of characters to be printed, the number of decimal places, or the number of significant digits (see below). Unlike the width specification, the precision specification can cause either truncation of the output value or rounding of a floating-point value.
e, E
The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded.
f
The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits.
g,G
The precision specifies the maximum number of significant digits printed.
s
The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed.

Example:

Related Topics:


File I/O functions

fread - read data from file

Synopsis:

[var, numElements] =fread(fHandle, sizeSpec, formatString)

var=fread(fHandle)

Description:

The fread function reads up to sizeSpec items of size defined by the formatString from the input file associated with fHandle and stores them in the output variable, var. The file pointer associated with the file (if any) is increased by the number of bytes actually read.

var=fread(fHandle) reads until end-of-file is reached.

sizeSpec
sizeSpec is an [rows, cols] matrix specifying the shape of the output data and the numebr of elements to read from the input file. If size is not specified it defaults to inf, i.e. the file is scanned until EOF is reached.
formatString
Control the number of bytes to read for each element of the output variable.
char
Single byte
schar, int8
Single signed byte
uchar, uint8
Single unsigned byte
short, int16
Short signed integer (two bytes)
ushort, uint16
Short unsigned integer (two bytes)
int, int32
Signed 32-bit integer (four bytes)
uint, uint32
Unsigned 32-bit integer (four bytes)
long
Signed 32-bit integer (four bytes) - PC format
ulong
Unsigned 32-bit integer (four bytes) - PC format
float, float32
Single precision floating point number (four bytes)
double, float64
Double precision floating point number (eight bytes)

Example:

Related Topics:


File I/O functions

fscanf -read data from file using C-style input formating

Synopsis:

[var, numElements] =fscanf(fHandle, formatString, sizeSpec)

var=fscanf(fHandle, formatString)

Description:

The fscanf function reads up to sizeSpec items of format defined by the formatString from the input file associated with fHandle and stores them in the output variable, var. The file pointer associated with the file (if any) is increased by the number of bytes actually read.

var=fread(fHandle, formatString) reads until end-of-file is reached.

sizeSpec
sizeSpec is an [rows, cols] matrix specifying the shape of the output data and the numebr of elements to read from the input file. If size is not specified it defaults to inf, i.e. the file is scanned until EOF is reached.

formatString specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The values are assigned to the elements of the output argument, var.

Format specification
consists of optional and required fields, has the following form: % [width]type
The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in the input file; the matching characters in the file are scanned but not stored. If a character in the file conflicts with the format specification, scanning terminates, and the character is left in the file as if it had not been read.
type
c
Single char
d
Decimal integer
e, E, f, g, G
Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent ("e" or "E") followed by an optionally signed integer value.
s
String, up to first white-space character (space, tab or newline).
width
Width is a positive decimal integer controlling the maximum number of characters to be read from the file for each element. No more than width characters are converted and stored at the corresponding argument element. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.

Example:

Related Topics:


File I/O functions

fseek - position the file read/write pointer

Synopsis:

fseek(fHandle, offset, originSpec)

Description:

The fseek function moves the file pointer (if any) associated with with fHandle to a new location that is offset bytes from originSpec. The next operation on the file takes place at the new location. On a file open for update, the next operation can be either a read or a write.

originSpec are as follows:

'b'
offset is specified from the beginning of file
'c'
offset is specified from the current file pointer position.
'e'
offset is specified from the end of file

Example:

Related Topics:


File I/O functions

ftell - read the position the file read/write pointer

Synopsis:

ftell(fHandle)

Description:

The ftell returns the file pointer position (if any) associated with with fHandle. On devices incapable of seeking (such as serail ports and/or printers), or when fHandle does not refer to an open file, the return value is -1.

Example:

Related Topics:


File I/O functions

fwrite - write data to file

Synopsis:

numElements=fwrite(fHandle, var, formatString)

Description:

The fwrite function writes all the elements of variable var to the output file associated with fHandle using the format specified by formatString. The file pointer associated with the file (if any) is increased by the number of bytes actually written.

sizeSpec

formatString

Example:

Related Topics:


File I/O functions

sprintf - write data to string using C-style output formating

Synopsis:

var=sprintf(formatString, arg1,...)

Description:

The sprintf formats the data of arg1 (and any additional variable listed after arg1) and writes a series of characters/values to the output variable, var. sprintf has the same functionality of fprintf but rather than writing to a file, the output is return via output variable.

Example:

Related Topics:


File I/O functions

sscanf -read data from string using C-style input formating

Synopsis:

var=sscanf(inputString, formatString, sizeSpec)

var=sscanf(inputString, formatString)

Description:

The sscanf function reads up to sizeSpec items of format defined by the formatString from the input string and stores them in the output variable, var.sscanf has the same functionality of fscanf but rather than reading from a file, fscanf reads from inputString.

Example:

Related Topics:


File I/O functions

_devctrl - device control command

Synopsis:

_devctrl(fHandle, "Mode", modeSpec) - control serial port mode setting

_devctrl(fHandle, "Timeout", timeoutSpec) - control serial port timeout setting

Description:

The _devctrl(fHandle, "Mode", modeSpec) set the mode for the serial port mode as specified by the modeSpec for the file associated with fHandle.

modeSpec
[baud=b] [parity=p] [data=d] [stop=s] [to=on|off] [xon=on|off] [odsr=on|off] [octs=on|off] [dtr=on|off|hs] [rts=on|off|hs|tg] [idsr=on|off]
baud=b
Specifies the transmission rate in bits per second. The following list shows valid abbreviations for b and its related rate:
                11   110 baud
                15   150 baud 
                30   300 baud 
                60   600 baud
                12   1200 baud 
                24   2400 baud 
                48   4800 baud
                96   9600 baud 
                19   19,200 baud
parity=p
Specifies how the system uses the parity bit to check for transmission errors. The p value can be one of the following: n (none), e (even), o (odd), m (mark), or s (space). The default value is e. Not all computers support the values m and s.
data=d
Specifies the number of data bits in a character. Valid values for d are in the range 5 through 8. The default value is 7. Not all computers support the values 5 and 6.
stop=s
Specifies the number of stop bits that define the end of a character: 1, 1.5, or 2. If the baud rate is 110, the default value is 2; otherwise, the default value is 1. Not all computers support the value 1.5.
to=on|off
Specifies whether infinite time-out processing is on or off. The default is off.
xon=on|off
Specifies whether the xon or xoff protocol for data-flow control is on or off.
odsr=on|off
Specifies whether output handshaking that uses the Data Set Ready (DSR) circuit is on or off.
octs=on|off
Specifies whether output handshaking that uses the Clear To Send (CTS) circuit is on or off.
dtr=on|off
Specifes whether the DTR circuit is on or off.
rts=on|off|hs|tg
Specifies whether the RTS circuit is set to on, off, handshake, or toggle.
idsr=on|off
Specifies whether the DSR circuit sensitivity is on or off.

The _devctrl(fHandle, "Timeout", timeoutSpec) set the timeout for the serial port mode as specified by the timeoutSpec for the file associated with fHandle.

timeoutSpec
Five element vector specifing the following values (in milliseconds): [ReadIntervalTimeout, ReadTotalTimeoutMultiplier; ReadTotalTimeoutConstant; WriteTotalTimeoutMultiplier; WriteTotalTimeoutConstant]. See Win32 documentation on the COMMTIMEOUTS structure is used in the SetCommTimeouts() and GetCommTimeouts() functions to set and query the time-out parameters for a communications device.

Example: _devctrl(comm_fid, 'Mode', 'baud=9600')
_devctrl(comm_fid, 'Mode', 'baud=1200 parity=N data=8 stop=1')
_devctrl(comm_fid, 'Timeout', [0, 0, 0, 0, 0])

Related Topics:


File I/O functions

<< >> Title Contents Index