Dong Guo: in-depth introduction to the hybrid programming of MATLAB 7.x
Binary files
MATLAB requires low-level file commands to read and write binary files. Like reading and writing a text file, using a low-level command to process a binary file involves three steps: Opening a file, reading and writing the corresponding file, and closing the file. Opening/closing a file is the same as a text file, but different parameters are required. Only the intermediate steps, that is, reading and writing files, need to use commands specifically for binary files.
Enable/disable a binary file
Fopen/fclose
Binary file read/write
Fread
Similar to fscanf
Fwrite
Similar to fprintf
File read/write location control: After a file is successfully opened, an internal Pointer Points to the first byte starting with the file. With the execution of the read/write commands on the file, this pointer will also move in the file, pointing to different locations in the file.
Fseek
Used to set the position of the file pointer
Ftell
Used to obtain the position of the pointer to the current file
Frewind
Used to reset the file pointer to the beginning of the file
Feof
Used to determine whether the object has reached the end of the file
Mat File
Read and Write mat files in MATLAB
The mat file is a binary data file used by Matlab. The mat file not only saves the variable data, but also saves the variable name and data type. Therefore, after loading a mat file in MATLAB, you can completely reproduce the variables when saving the mat file in the current MATLAB workspace. This is one reason why mat files are better than other file formats.
Load
Load is a built-in function of MATLAB. Its function is to import data from mat files or text files.
Although the mat file is a binary file, it can be used on different computer platforms. Save stores binary data in bytes in the mat file.
Save
Save is also a built-in function of MATLAB. Its function is to store one or more variables in the current MATLAB workspace into an external file.
Save can also be used on different computer platforms and can correctly read data based on the byte storage sequence of binary data on the target computer.
Read and Write mat files in C ++
Using MATLAB mat programming interface functions
Matopen
Matclose
Matgetdir
Matgetfp
Matgetvariable
Matputvariable
Matgetnextvariable
Matdeletevariable
Matputvariableasglobal
Matgetvariableinfo
Matgetnextvariableinfo
The header file mat. h contains the definitions of these functions. The library function libmat. LIB contains the function code, and libmat. Lib depends on libmx. Lib.
Read the content of the mat file in a common C ++ Program
This section describes how to use the interface functions provided by MATLAB to read and write mat files in C/C ++. However, the developed program requires the dynamic link library function provided by MATLAB during runtime. Mathworks provides a dynamic link library corresponding to MATLAB 6.x, but does not provide a dynamic link library for Matlab 7.x.