C++ method to read and write mat files

  • 2020-04-01 21:38:05
  • OfStack

Recently in the process of writing C++ program, need to use matlab-generated. Mat file, so find the basic use method.  

My computer environment is win7 64-bit system, VS2010, matlab R2010b.  

I. engineering configuration:  
1. Vc + + directories - > Contains directory addition : 
MATLAB \ R2010b \ extern \ include 
MATLAB \ R2010b \ extern \ include \ win64  
  - > Library directory added:  
  MATLAB \ R2010b \ extern \ lib \ win64 \ microsoft 
  MATLAB \ R2010b \ extern \ lib \ win32 \ microsoft 
2. C/C + + - > Conventional - > Additional include directory add : 
  MATLAB \ R2010b \ extern \ include 
  MATLAB \ R2010b \ extern \ include \ win64  
3. Linker -> Input - > Additional dependency libraries added : 
Libmat. Lib 
Libmx. Lib 
Libmex. Lib 
Libeng. Lib 

Since the installed matlab is 64-bit, to call its function, the project needs to be converted to X64& PI;
4. Top menu -> Generate - > Configuration manager -> Platform: X64  
5. Linker -> Advanced - > Target computer : 
MachineX64 (/ MACHINE: X64)  

6. Computer environment variables -> Path adds:  
E: \ DevTools \ \ MATLAB R2010b \ extern \ lib \ win64 \ Microsoft;  
E: \ DevTools \ \ MATLAB R2010b \ bin \ win64.  

Ii. Code example of basic reading and writing mat file:

You first need to include the header file:  


#include <mat.h>  
[cpp] view plaincopy  
    MATFile *pmatFile = NULL;    
    mxArray *pMxArray = NULL;    

    //Read the. Mat file (example: the name of the mat file is "initUrban. Mat ", which contains "initA") & NBSP;    
    double *initA;    

    pmatFile = matOpen("initUrban.mat","r");    
    pMxArray = matGetVariable(pmatFile, "initA");    
    initA = (double*) mxGetData(pMxArray);    
    M = mxGetM(pMxArray);    
    N = mxGetN(pMxArray);    
    Matrix<double> A(M,N);    
    for (int i=0; i<M; i++)    
        for (int j=0; j<N; j++)    
            A[i][j] = initA[M*j+i];    

    matClose(pmatFile);    
    mxFree(initA);    

    //Generate. Mat file & NBSP;    
    double *outA = new double[M*N];    
        for (int i=0; i<M; i++)    
            for (int j=0; j<N; j++)    
                outA[M*j+i] = A[i][j];    
    pmatFile = matOpen("A.mat","w");    
    mxSetData(pMxArray, outA);    
    matPutVariable(pmatFile, "A", pMxArray);    
    matClose(pmatFile);    

Description of procedures  

1. Use the matOpen function to open the mat file  


MATFile *matOpen(const char *filename,const char *mode)& PI;

Mode:  
R: opened read-only  
U: update mode, readable and writable, but no new file is created if the data file you want to open does not exist  
W: open as a write, write only as in, if the data file you want to open does not exist, create a new file  


2. Use the matGetVariable function to read the variable & PI in the mat file;

MxArray * matGetVariable(MATFile * pMF, const char * name);  

Reads a variable named name, returns a data array pointer  


3. Use the mxGetData function to get the data & PI in the data array;

Void *mxGetData(const mxArray *pa);  

You need to use a cast when you return.  


4. Use mxGetM and mxGetN functions to obtain the dimension & PI of the data array matrix;

Size_t mxGetM (const mxArray * pa);  

Size_t mxGetN (const mxArray * pa);  


5. Use the mxSetData function to store variables in the data array  

Void mxSetData(mxArray *pa, void  * newdata);  


6. Use the matPutVariable function to store the data array in the mat file & PI;

Int matPutVariable(MATFile * pMF, const char * name, const mxArray * pA);  

Save success returns 0, save error returns non-0 & PI;


7. Since the matrix in malab is stored in columns, which is different from that in c, the matrix obtained from mat file needs to be rearranged. Note the same when generating mat files.  


Related articles: