MATLAB file operations and reading TXT files

Source: Internet
Author: User
MATLAB File Operations

File operations are an important input/output method, that is, reading data from a data file or writing results into a data file. MATLAB provides a series of low-level input and output functions for file operations.
1. Open and Close a file
1) open the file
Before reading and writing a file, you must use the fopen function to open or create the file and specify the operation method for the file. The call format of the fopen function is:
FID = fopen (file name, 'open method ')
Note: FID is used to store the file handle value. If the returned handle value is greater than 0, the file is successfully opened. The file name uses a string to indicate the data file to be opened. Common open methods are as follows:
'R': open a file in read-only mode (default mode). The file must already exist.
'R + ': open the file in read/write mode. Read the file before writing it. The file must already exist.
'W': write data after opening. If the file already exists, update it. If the file does not exist, create it.
'W + ': open a file in read/write mode. First read and then write. If the file already exists, update it. If the file does not exist, create it.
'A': add data at the end of the opened file. If the file does not exist, it is created.
'A + ': After opening the file, read the data before adding the data. If the file does not exist, it is created.
In addition, add a "T" after these strings, such as 'rt 'or 'wt +', to open the file in text; if "B" is added ", it is opened in binary format, which is also the default open method of the fopen function.
2) close the file
After reading and writing a file, close it in time to avoid data loss. Use the fclose function to close the file. The call format is:
Sta = fclose (FID)
Note: This function disables the files represented by FID. Sta indicates the code returned when the file operation is disabled. If the operation is successful, 0 is returned; otherwise,-1 is returned. To close all opened files, use fclose ('all ').
2. Binary file read/write operations
1) Write binary files
The fwrite function writes elements in the Matrix to a file based on the specified data precision. The call format is:
Count = fwrite (FID, A, precision)
Description: Here, Count returns the number of data elements (which can be defaulted), FID is the file handle, and A is used to store the data written into the file. Precision indicates the data precision. Common data precision is as follows: char, uchar, Int, long, float, double, etc. The default data precision is uchar, that is, the unsigned character format.
Example 6.8 store a binary matrix into a disk file.
> A = [1 2 3 4 5 6 7 8 9];
> FID = fopen ('d:/test. bin', 'wb ') % open the file by writing binary data
FID =
3% if its value is greater than 0, it indicates that it is successfully opened.
> Fwrite (FID, A, 'double ')
Ans =
9% indicates that nine data entries are written.
> Fclose (FID)
Ans =
0% indicates closing successful
2) read binary files
The fread function can read data from binary files and store the data to the matrix. The call format is:
[A, Count] = fread (FID, size, precision)
Description: A is the matrix used to store read data, count is the number of data elements read, FID is the file handle, and size is optional, if this option is not selected, the entire file is read. If this option is selected, its values can be n (read n elements to a column vector) and INF (read the entire file), [M, N] (read data to the m × n matrix and store data by column ). Precision is used to control the precision of written data in the same form as the fwrite function.
3. read/write operations on text files
1) read text files
The fscanf function can read the content of a text file and store it in a matrix in the specified format. The call format is:
[A, Count] = fscanf (FID, format, size)
Description: A is used to store the read data. Count returns the number of read data elements. FID is the file handle. format is used to control the format of the read data. It consists of % and a format character, common Format characters include D (integer type), F (floating point type), S (string type), and C (character type, you can also insert additional format specifiers, such as data width descriptions, between % and format operators. Size is optional and determines the data arrangement in matrix A. It can take the following values: n (read n elements to a column vector), INF (read the entire file), [M, N] (read data to the m × n matrix and store data by column ).
2) Write a text file
The fprintf function can write data to a text file in the specified format. The call format is:
Fprintf (FID, format,)
Description: FID is the file handle and specifies the file to write data. format is used to control the format of the written data. It is the same as the fscanf function. A is the matrix used to store data.
In Example 6.9, create a character matrix and store it to the disk. Then read and assign values to another matrix.
> A = 'string ';
> FID = fopen ('d:/char1.txt ', 'w ');
> Fprintf (FID, '% s', );
> Fclose (FID );
> Fid1 = fopen ('d:/char1.txt ', 'rt ');
> Fid1 = fopen ('d:/char1.txt ', 'rt ');
> B = fscanf (fid1, '% s ')
B =
String

MATLAB reads TXT files

Fidgefopen('fx.txt ', 'R ');
% Get the file number
[F, Count] = fscanf (FID, '% F % F', [12, 90]);
% Read the data of file number 1 to F. Where F is the matrix of [12 90]
% Here '% F % F' indicates the Data Reading situation. It is read Based on the original data type.
Fclose (FID );
% Close file
In addition, some TXT files can be opened using load.
The statement is
F=load('fx.txt)
I personally think the first method is better. For some time, it is special to use the load
It is quite troublesome.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.