MAT-files for MATLAB

1. MAT-file

  • MAT-files are the default file format for MATLAB data storage, saving data in double-precision binary format.
  • MAT-files provide a sharing mechanism for other programming languages ​​(such as C, C++, FORTRAN, etc.) to use MATLAB data.

1. MAT-file

  • A MAT-file consists of a 128-byte MAT-file header followed by data units.
  • The file header includes information such as the version of MATLAB and when the file was created.
  • The data unit is divided into two parts: flag and data. The flag occupies 8 bytes and contains information such as data type and data size. If the number of data bytes in the flag is less than 4, MATLAB uses a compressed format to store the data in the cell.
  • The MATLAB savecommand can save the internal data of the MATLAB system as a MAT file, and loadthe command can read the data in the MAT file on the disk into the MATLAB system.
  • In addition, in order to effectively manage MAT-files, and to read and create MAT-files outside of MATLAB, MATLAB provides a library of subroutines, which users can directly call in C, C++, FORTRAN programs to create and read MAT-file.
  • The API functions provided by MATLAB for manipulating MAT-files are packaged in two standard library files: libmat.liband libmx.lib. The former is used for operations on MAT-files, and the latter is used for operations on matrices in MAT-files.

2. C language MAT function

  • The C language MAT function is used to operate the MAT file in the C program. The details of C language MAT functions and functions are shown in the table below.
MAT function function function
matOpen Open MAT-file
matClose Close MAT-file
matGetDir Get list of matrices in MAT-file
matGetV ariable Read a matrix from a MAT-file
matPutV ariable write a matrix to a MAT-file
matGetNextV ariable Read the next matrix from the MAT-file
matDeleteV ariable remove - matrix from MAT-file
matPutVariableAsGlobal writes a matrix to a MAT-file, and puts it into the global workspace when the matrix is ​​subsequently read from the file
matGetV ariableInfo Read the flag information of the data unit from the MAT-file
matGetNextV ariableInfo Read the flag information of the next data unit from the MAT-file

3. Basic operation of MAT-file

  • Below, we take the C language as an example to illustrate the operation method of the MAT file.
  • In the C program, the file is operated through the pointer to the MAT file, and the format of defining the pointer to the MAT file is as follows:
MATFile *mfp;
  • The MAT file operation function is mat.hdefined in the header file, so the following command should be used at the beginning of the C program:
#include "mat.h"
  • MAT-file operations are divided into the following three types.

3.1 Open MAT-file

  • To open a file, use matOpenthe function, and its calling format is as follows:
mfp=matopen(filename,mode)
  • Among them, mfp is a pointer that has been declared as a MAT file type, filename is the MAT file to be operated, and mode is used to describe how to use the file, and the following values ​​can be taken.
  • (1) r: Open the file in read-only mode.
  • (2) u: Open the file in readable and writable mode.
  • (3) w: Open the file in a write-only mode. If there is content in the file, delete the original content.
  • (4) wz: Open the file for writing compressed data.
  • If the function is successfully opened, the function handle is returned, otherwise NULL is returned.

3.2 Reading and writing MAT-files

  • (1) Write a matrix to the MAT-file. The function call format is as follows:
matPutVariable(mfp,name, mp)
matPutArrayAsGlobal(mfp,mp)
  • Among them, mfp is the defined MAT file pointer, name is the name of the mxArray type data written in the file in the file, and mp is the pointer to the mxArray type variable in the work area.
  • If an mxArray with the same name as name exists in the file, the original value will be overwritten.
  • If no mxArray with the same name exists, this mxArray is added to the end of the file.
  • If the function executes successfully, it returns 0, otherwise it returns a non-zero value. After the second function is called, when the MAT-file is loaded with the load command, the variables corresponding to the matrix become global variables.
  • (2) Obtain the variable list in the MAT-file. The function call format is as follows:
matGetDir(mfp,num)
  • Among them, mfp is the defined MAT-file pointer, and num is the pointer to the system variable that records the number of mxArray in the file.
  • If the function is executed successfully, mfp returns an array of character pointers, each element of which points to a matrix in the MAT-file.
  • Execution fails, mfp returns a null pointer, num is -1. If num=0, there are no matrices in the MAT-file.
  • (3) Read a matrix from a MAT-file. The function call format is as follows:
matGetVariable(mfp,name)
  • Among them, mfp is the defined MAT-file pointer. If the function executes successfully, create an object of type mxArray named name in the memory, and assign the read data to the object.
  • matGetDir, matGetVariableThe function allocates memory through the mxCalloc function, and must use the function to release the memory at the end of the program mxFree.
  • (4) Delete a matrix from the MAT-file. The function call format is as follows:
matDeleteVariable(mfp,name)
  • Among them, mfp is the defined MAT-file pointer, and name is the matrix to be deleted. If the function executes successfully, it returns 0, otherwise it returns a non-zero value.

3.3 Closing the MAT-file

  • To close the MAT-file, use matClosethe function, whose calling format is as follows:
matClose(mfp)
  • Among them, mfp is the defined MAT-file pointer.
  • If the function executes successfully, it returns 0, otherwise it returns EOF.

3.4 mx function

  • In the C program, when using MATLAB data, mxthe functions in the interface functions provided by MATLAB are also used to complete the operation on the mxArray object.
  • The matrix operation of MATLAB is built on the core of the mxArray structure (mwArray class in C++), and the definition of the mxArray structure is in the extern\include\matrix.h file of MATLAB. The following table shows the commonly used C language mxfunctions and functions. mxFor the usage of other functions, please refer to the MATLAB help file.
C language mx function Function
char *mxArrayToString(const mxArray *array_ ptr); Convert the mxArray structure to a string
mxArray *mxCreateDoubleMatrix(int ​​m,int n,mxComplexity ComplexFlag); Create a two-dimensional double-precision type mxArray matrix
mxArray *mxCreateString(const char *str) create string
void mxDestroyArray(mxArray *array_ ptr); Free the memory allocated by the mxCreate class function
int mxGetM(const mxArray *array_ ptr); Get the number of rows of the matrix
int mxGetN(const mxArray *array_ ptr); Get the number of columns of the matrix
void mxSetM(mxArray *array_ ptr,int m); Set the number of rows of the matrix
void mxSetN(mxArray *array_ ptr,int m); set the number of columns of the matrix
double *mxGetPr(const mxArray *array_ ptr); Get the data pointer of the real part of the matrix
double *mxGetPi(const mxArray *array_ ptr); Get the data pointer of the imaginary part of the matrix
void mxSetPr(mxArray *array_ ptr,double *pr); Set the data pointer of the real part of the matrix
void mxSetPi(mxArray *array_ ptr,double *pr); Set the data pointer of the imaginary part of the matrix
void *mxCalloc(size_ t n,size_ t size); Allocate n units of size size bytes in memory and initialize them to 0

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/131518050