help fseek
FSEEK Set file position indicator.
STATUS = FSEEK(FID, OFFSET, ORIGIN) repositions the file position
indicator in the file associated with the given FID. FSEEK sets the
position indicator to the byte with the specified OFFSET relative to
ORIGIN.
FID is an integer file identifier obtained from FOPEN.
OFFSET values are interpreted as follows:
>= 0 Move position indicator OFFSET bytes after ORIGIN.
< 0 Move position indicator OFFSET bytes before ORIGIN.
ORIGIN values are interpreted as follows:
'bof' or -1 Beginning of file
'cof' or 0 Current position in file
'eof' or 1 End of file
STATUS is 0 on success and -1 on failure. If an error occurs, use
FERROR to get more information.
Example:
fseek(fid,0,-1)
"rewinds" the file.
fseek用于定位
1)读
>> fid=fopen('test.m','r')
fid =
3
>> fseek(fid,5,'bof')
ans =
0
>> a=fread(fid,5,'uint8=>char')
a =
1
0
;
>>
>> fseek(fid,0,'bof')
ans =
0
>> a=fread(fid,5,'uint8=>char')
a =
P
=
[
1
>>
2)写
>> fid=fopen('test.m','r+')
fid =
3
>> fseek(fid,0,'eof')
ans =
0
>> fwrite(fid,'%增加一行','char')
ans =
5
>> fclose(fid)
ans =
0
>>
定位于当前位置写,注意这种写是改写
>> fid=fopen('test.m','r+')
fid =
3
>> fseek(fid,0,'cof')
ans =
0
>> ftell(fid)
ans =
0
>> fwrite(fid,'%增加上面一行','char')
ans =
7
>> fclose(fid)
ans =
0
>>
ftell得到当前位置
help ftell
FTELL Get file position indicator.
POSITION = FTELL(FID) returns the location of the file position
indicator in the specified file. Position is indicated in bytes
from the beginning of the file. If -1 is returned, it indicates
that the query was unsuccessful. Use FERROR to determine the nature
of the error.
FID is an integer file identifier obtained from FOPEN.
3、读取行
1)fgetl
>> fid=fopen('test.m','r')
fid =
4
>> mline=fgetl(fid)
mline =
%增加上面一行 1];
>> mline=fgetl(fid)
mline =
T=[0 1 0];
>> mline=fgetl(fid)
mline =
w=[0 0 ];
>> mline=fgetl(fid)
mline =
[S,Q]=size(T)
>> mline=fgetl(fid)
mline =
b=0;
>>
2)fgets
与fgetl的区别就是,fgets是保留换行符读取一行
>> mline=fgets(fid)
mline =
A=purelin(w*P+b);
>>
4、格式化读写ascii
1)读
>> a=fscanf(fid,'%s',10)
a =
e=T-A;LP.lr=maxlinlr(P)%误差平方和sse=sumsqr(e);whilesse>0.0000001dW=learnwh([],P,[],[],[],[],e,[],[],[],LP,[]);dB=learnwh(b,ones(1,Q),[],[],[],[],e,[],[],[],LP,[]);
>>
>> a=fscanf(fid,'%s')
a =
w=w+dW;b=b+dB;A=purelin(w*P+b)e=T-Asse=sumsqr(e)end%增加一行
>>
help fscanf
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.
If %s is used an element read may cause several MATLAB matrix
elements to be used, each holding one character. Use %c to read
space characters; the format %s skips all white space.
MATLAB reads characters using the encoding scheme associated with
the file. See FOPEN for more information. If the format string
contains ordinary characters, MATLAB matches each of those characters
with a character read from the file after converting both to the
MATLAB internal representation of characters.
Mixing character and numeric conversion specifications causes the
resulting matrix to be numeric and any characters read to show up
as their numeric values, one character per MATLAB matrix element.
FSCANF differs from its C language namesake in an important respect -
it is "vectorized" in order to return a matrix argument. The format
string is recycled through the file until an end-of-file is reached
or the amount of data specified by SIZE is read in.
Examples:
S = fscanf(fid,'%s') reads (and returns) a character string.
A = fscanf(fid,'%5d') reads 5-digit decimal integers.
2)写
>> fid=fopen('test.m','r+')
fid =
4
>> fseek(fid,0,'eof')
ans =
0
>> x=[123,3452]
x =
123 3452
>> fprintf(fid,'%d %6.2f',x)
ans =
11
>> fclose(fid)
ans =
0
>>
help fprintf
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
最后是关闭文件
help fclose
FCLOSE Close file.
ST = FCLOSE(FID) closes the file associated with file identifier FID,
which is an integer value obtained from an earlier call to FOPEN.
FCLOSE returns 0 if successful or -1 if not. If FID does not represent
an open file, or if it is equal to 0 (standard input), 1 (standard
output), or 2 (standard error), FCLOSE throws an error.
ST = FCLOSE('all') closes all open files, except 0, 1 and 2.
See also fopen, fclose, fscanf, fread, fwrite, sprintf, disp.