1, MATLAB file operation mainly has three steps: first open the file, then read and write to the file, and finally close the file.
2, Fid=fopen (file name, open mode)
' R ' |
Read-only, file must exist (default open mode) |
' W ' |
Write the file, if the file already exists, the original content will be overwritten; If the file does not exist, create a new one |
A |
Add at the end of the file and create a new file if it does not exist |
' R+ ' |
Readable and writable, file must exist |
' w+ ' |
Readable and writable, if the file already exists, the original content will be overwritten; If the file does not exist, create a new one. |
' A + ' |
Readable writable can be added, if the file does not exist create a new |
3, FID is a file handle, other functions can use it to manipulate the file. If the handle value is greater than 0, the file opens successfully, and if open fails, the FID return value is-1.
Fid=fopen (' output.txt ', ' wt+ ');
fprintf (FID, ' Hello world!\n ');
Fclose (FID);
4, there are two standard code files, do not need to open directly to use, respectively: fid=1 standard output file, fid=2 standard error file.
fprintf (1, ' Hello world!\n ');
fprintf (2, ' Hello world!\n ');
5. If the open mode is not specified, it is read-only.
6. The file is in two formats: binary (b) and text (t).
6. Under Windows, the default for open files is binary format, and if you want to open them as text, you must add the character ' t ' in the hit mode.
Fid=fopen (' output.txt ', ' wt+ ');
7, where FID is the handle of the file to be closed, the status is the return code of the closed file, if the success of the shutdown is 0, otherwise-1.
Status=fclose (FID);
8, the text file read
[A,count]=fscanf (Fid,format,size)
A used to hold the read data
Count returns the number of read data, as an option
FID to file handle
The size is optional, and the default is to read the entire file,
If given, the value can be:
N |
Read N data to a column vector |
Inf |
Read the entire file |
[M,n] |
Reads m´n data into a m´n matrix, which is stored by column |
Format is used to control read data formats, consisting of% plus format characters, and the following table is a common format character, which can be followed by a data width after%, such as%12d
C |
Character type |
G |
Floating point number (automatic) |
D |
Decimal integer |
O |
Octal |
E |
Floating point number (scientific counting method) |
S |
String |
F |
Floating-point number (decimal form) |
X |
Hexadecimal |
9. Writing a text file
count=fprintf (fid,format, output variable list)
- Writes an output variable to a file in the specified format
- If you omit the FID, it means that the output on the screen
- Count returns the number of data elements written (can be saved)
- Format begins with a%, and usually consists of the following parts:
L fprintf format specifier for command
C |
Character type |
G |
Floating point number (automatic) |
D |
Decimal integer |
O |
Octal |
E |
Floating point number (scientific counting method) |
S |
String |
F |
Floating-point number (decimal form) |
x/x |
Hexadecimal |
L special characters that can also be used in format
\b |
Back up a grid. |
\ t |
Horizontal tab |
\f |
Page change |
\\ |
Back slash |
\ n |
Line break |
‘‘ |
Single quotation marks |
\ r |
Enter |
%% |
Percent Sign |
10. Location of Reading and writing
- When you open a file to read and write data, you need to determine and control the file read and write location, such as whether the data read, or read and write to the specified location data.
- When reading and writing files, Matlab automatically creates a file location pointer to manage and maintain the starting position of the file's read and write data.
- Matlab provides several file location pointer positioning operation functions: Fseek, Ftell, Frewind, and feof
Fseek:
Status=fseek (Fid,offset,origin)
FID to file handle
Offset indicates the number of bytes relative to the position pointer offset, or offset to the end of the file if the negative representation is shifted to the header direction
Origin represents the reference position of the position pointer movement, and there are three kinds of values:
' COF ' indicates the current position,
' BOF ' indicates where the file was started,
' EOF ' indicates the end of the file;
If the position is successful, the status return value is 0, otherwise return-1
Ftell, Frewind and feof:
The return value is the number of bytes from the beginning of the file to the current position of the pointer: Position=ftell (FID)
Returns the position pointer to the starting position of the file: Frewind (FID)
Determines whether the file position pointer reaches the end of the file, returns 1 if the file position pointer is already at the end of the file, or returns 0:eofstat=ftell (FID)
Reference: http://www.cnblogs.com/longdouhzt/archive/2013/01/29/2881749.html
MATLAB file operations