Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
09.02.2015
Размер:
324.1 Кб
Скачать

Formatted file I/o

<fgetl> - Read line from file, discard newline character.

FGETL Read line from file, discard newline character.

TLINE = FGETL(FID) returns the next line of a file associated with file

identifier FID as a MATLAB string. The line terminator is NOT

included. Use FGETS to get the next line with the line terminator

INCLUDED. If just an end-of-file is encountered, -1 is returned.

If an error occurs while reading from the file, FGETL returns an empty

string. Use FERROR to determine the nature of the error.

MATLAB reads characters using the encoding scheme associated with the

file. See FOPEN for more information.

FGETL is intended for use with files that contain newline characters.

Given a file with no newline characters, FGETL may take a long time to

execute.

Example

fid=fopen('fgetl.m');

while 1

tline = fgetl(fid);

if ~ischar(tline), break, end

disp(tline)

end

fclose(fid);

See also fgets, fopen, ferror.

Overloaded methods:

serial/fgetl

icinterface/fgetl

udp/fgetl

Reference page in Help browser

doc fgetl

<fgets> - Read line from file, keep newline character.

FGETS Read line from file, keeping the newline character.

TLINE = FGETS(FID) returns the next line of a file associated

with file identifier FID as a MATLAB string. The line

terminator is included. Use FGETL to get the next line

WITHOUT the line terminator. If just an end-of-file is

encountered then -1 is returned.

If an error occurs while reading from the file, FGETS returns an empty

string. Use FERROR to determine the nature of the error.

TLINE = FGETS(FID, NCHAR) returns at most NCHAR characters of

the next line. No additional characters are read after the

line terminator(s) or an end-of-file.

MATLAB reads characters using the encoding scheme associated

with the file. See FOPEN for more information.

FGETS is intended for use with files that contain newline characters.

Given a file with no newline characters, FGETS may take a long time to

execute.

See also fgetl, fopen, ferror.

Overloaded methods:

serial/fgets

Icinterface/fgets

udp/fgets

Reference page in Help browser

doc fgets

<fprintf> - Write formatted data to file.

FPRINTF Write formatted data to text file.

FPRINTF(FID, FORMAT, A, ...) applies the FORMAT to all elements of

array A and any additional array arguments in column order, and writes

the data to a text file. FID is an integer file identifier. Obtain

FID from FOPEN, or set it to 1 (for standard output, the screen) or 2

(standard error). FPRINTF uses the encoding scheme specified in the

call to FOPEN.

FPRINTF(FORMAT, A, ...) formats data and displays the results on the

screen.

COUNT = FPRINTF(...) returns the number of bytes that FPRINTF writes.

FORMAT is a string that describes the format of the output fields, and

can include combinations of the following:

* Conversion specifications, which include a % character, a

conversion character (such as d, i, o, u, x, f, e, g, c, or s),

and optional flags, width, and precision fields. For more

details, type "doc fprintf" at the command prompt.

* Literal text to print.

* Escape characacters, including:

\b Backspace '' Single quotation mark

\f Form feed %% Percent character

\n New line \\ Backslash

\r Carriage return \xN Hexadecimal number N

\t Horizontal tab \N Octal number N

For most cases, \n is sufficient for a single line break.

However, if you are creating a file for use with Microsoft

Notepad, specify a combination of \r\n to move to a new line.

Notes:

If you apply an integer or string conversion to a numeric value that

contains a fraction, MATLAB overrides the specified conversion, and

uses %e.

Numeric conversions print only the real component of complex numbers.

Example: Create a text file called exp.txt containing a short table of

the exponential function.

x = 0:.1:1;

y = [x; exp(x)];

fid = fopen('exp.txt','w');

fprintf(fid,'%6.2f %12.8f\n',y);

fclose(fid);

Examine the contents of exp.txt:

type exp.txt

MATLAB returns:

0.00 1.00000000

0.10 1.10517092

...

1.00 2.71828183

See also fopen, fclose, fscanf, fread, fwrite, sprintf, disp.

Overloaded methods:

serial/fprintf

icinterface/fprintf

Reference page in Help browser

doc fprintf

<fscanf> - Read formatted data from file.

FSCANF Read formatted data from file.

[A,COUNT] = FSCANF(FID,FORMAT,SIZE) reads data from the file specified

by file identifier FID, converts it according to the specified FORMAT

string, and returns it in matrix A. COUNT is an optional output

argument that returns the number of elements successfully read.

FID is an integer file identifier obtained from FOPEN.

SIZE is optional; it puts a limit on the number of elements that

can be read from the file; if not specified, the entire file

is considered; if specified, valid entries are:

N read at most N elements into a column vector.

inf read at most to the end of the file.

[M,N] read at most M * N elements filling at least an

M-by-N matrix, in column order. N can be inf, but not M.

If the matrix A results from using character conversions only and

SIZE is not of the form [M,N] then a row vector is returned.

FORMAT is a string containing ordinary characters and/or C language

conversion specifications. Conversion specifications involve the

character %, optional assignment-suppressing asterisk and width

field, and conversion characters d, i, o, u, x, e, f, g, s, c, and

[. . .] (scanset). Complete ANSI C support for these conversion

characters is provided consistent with 'expected' MATLAB behavior.

For a complete conversion character specification, see the Language

Reference Guide or a C manual.

Соседние файлы в папке Библиотеки Matlab