Dong Guo: in-depth introduction to the hybrid programming of MATLAB 7.x
MATLAB provides a variety of functions that can read and write files. These functions are part of the MATLAB Language and do not require any additional toolbox support.
Text Files
The file read/write functions in MATLAB can be divided into two categories: advanced functions and low-level functions. In short, the syntax for calling advanced functions is simple and easy to use. However, poor customization is applicable only to file types in some special formats and lacks flexibility.
In practice, many people recommend this option: when reading files, use advanced functions whenever possible; when saving and writing files, use low-level functions whenever possible, this is because when advanced functions store and write files, the file format is relatively simple.
Advanced commands
Common advanced functions for reading and writing text files include:
(1) read: Load
Load is a built-in function in MATLAB. Its main function is to input data into the MATLAB workspace from a text file or mat file.
If the number of columns in each row is not equal, an error occurs.
Load can be called in two ways:
Command Format:
Load bar. dat
Function Format:
[X] = load ('bar. dat ');
(2) read: importdata
It is suitable for reading data from text files or binary files in special formats (such as. wav. Importdata can be used to read files containing text instructions. User-Defined characters can be used as separators between data in each line of the file. If this parameter is not specified, spaces are automatically used as separators. If the number of columns in each row is different, importdata does not terminate the execution, but determines the size of the output matrix based on the number of columns in the first row. The missing columns are replaced by Nan.
(3) read: csvread
Used to read data files separated by commas.
(4) read: strread
(5) read: dlmread
The file cannot contain instructions. However, dlmread can not only read data files separated by commas, but also read files separated by other characters.
A variable in MATLAB. Mat data is saved as txt
Http://blog.csdn.net/boyhailong/article/details/7046488
(6) read: textread
Implemented by calling a Mex file named dataread. dll. Compared with the previous advanced functions for reading files, textread also has the advantages of simple calling syntax, and is more customizable for reading files. Textread can not only process files in fixed format, but also non-format files. It can also read data in each row of files one by column. It is usually used to process files in known formats. {Name, grade, X, Y, answer}
Note: textread will be removed in a future version. Use 'textscan' instead. (MATLAB 2011a)
(7) Write: Save
Save is also a built-in function of MATLAB. It is used to store one or more variables of the current MATLAB workspace into external files. By default, the SAVE command saves and writes data in mat format, but you can also specify the text storage mode.
Note: Save cannot save the plural variable in a text file. If there are multiple numbers, save only saves their real parts. In addition, for structural variables, save can only store structural scalar values, but not arrays of structural variables.
'-M' |
Binary mat-file format (default ). |
'-ASCII' |
8-digit ASCII format. |
'-ASCII', '-Tabs' |
Tab-delimited 8-digit ASCII format. |
'-ASCII', '-double' |
16-digit ASCII format. |
'-ASCII', '-double','-Tabs' |
Tab-delimited 16-digit ASCII format. |
For more flexibility in creating ASCII files, use 'dlmwrite' or 'fprintf'. (MATLAB 2011a)
(8) Write: dlmwrite
Write matrix to ascii-delimited file
Low-level commands
These commands are very similar to the File Read and Write Functions in C. The procedure generally includes opening a file, reading and writing a file, and closing a file.
Common low-level commands for file read/write include the following seven:
(1) fopen
Fopen can open a file and obtain information from the opened file. By default, fopen opens a file in binary format.
NOTE: If "+" is used in the file opening mode, such as "A + T" and "W + T", the read and write operations on the file must be separated by fseek or frewind.
(2) fclose
It is recommended to close the file after it is opened and used to facilitate other operations on the file. After you use fopen to open a file, the system will mark the file as "in use". If you use fclose, this mark will be cleared. Otherwise, modification and deletion of the file will be affected.
(3) fgetl
Can read a row of content in a specified file, but does not include a new line separator. After opening a file with fopen, the file is read at the beginning of the file. After fgetl is called each time, the read location is automatically updated to the next line until the end of the file.
(4) fgets
Similar to fgetl, fgets reads the next row of content corresponding to the current file reading location. The difference is that fgets will contain the new line flag of the row.
(5) fscanf
Fscanf provides more flexibility to read formatted text files.
Note: After you use % s to read a string, each character in it is regarded as an element in the returned matrix. In addition, % s ignores the Space key in the file. To read spaces, % C must be used.
(6) textscan
Textscan is a new built-in function of MATLAB 7.0. It can read text file data more efficiently and flexibly, similar to advanced command textread, but textcan can better process large files. Textscan can read data from anywhere in the file. textscan provides more options for data conversion.
Note: The maximum value of data read by textscan is uint32 (4294967295). If the number in the file is greater than this value, it is replaced by this limit number.
(7) fprintf
Fprintf can print text information in a format specified by the user similar to the functions in ansi c language. Depending on the call parameters, fprintf can output results in a file or on the screen.
Write Data to text file