Matlab+Qt development notes: QT opens the mat file to display the read data


   the basic environment is introduced. The final purpose is to read and display the. Mat file. This article reads the mat file and displays it.



  the mat file tested is of type double.

Matlab library data type

Variable type: matError, error variable

  error type, actually an integer.

Variable type: MATFile, mat file pointer

   open the pointer returned by the mat file. The operation file needs to be used until it is closed.

Variable type: mxArray, mat array

  two dimensional array type generated by opening mat file.


Matlab library function is used to open and read

   some functions are actually sorted out, but this chapter only lists the functions that need to be used in this chapter (marked with "*")

*Function: matOpen, open mat file

MAT_API MATFile* matOpen(const char *filename, const char * mode);

  open the MAT file "file name" using "mode". Returns a pointer to the MAT file for other MAT API functions.
  currently supported modes:

  • "r" - read only
  • "w" - write only (delete existing files)
  • "w4" – same as "w", but create MATLAB 4.0 MAT file
  • "w7.3" – same as "w", but create MATLAB 7.3 MAT file
  • "u" - update the file, allow reading and writing, and do not delete the existing file

Function: matGetDir to get the list of group names

char ** matGetDir(MATFile * pMF, int *num);

  get the list of array names in MAT file. The string array returned by this function contains the "num" item. It is allocated by a call to mxCalloc, so it can (and must) be released by a call to mxFree.

*Function: matGetNextVariable to get the next variable array

mxArray * matGetNextVariable(MATFile *pMF, const char **nameptr);

  read the next array value from the current file location of MAT file pMF. This function can only be used with matOpen and matClose. Passing pMF to any other API function will cause matGetNextVariable() to not work properly.
   special note: if the variable table is polled, the second parameter is directly passed in as 0.

Function: matGetNextVariableInfo to get the next variable array

mxArray * matGetNextVariableInfo(MATFile *pMF, const char **nameptr);

  read the array header of the next array value in the MAT file. This function can only be used with matOpen and matClose. Passing pMF to any other API function will cause matGetNextVariableInfo to not work properly.
  for definitions, see the description of matGetVariableInfo() and the effective use of array headers.
    note: this function is the same as matGetNextVariable, but cannot get entity through mxGetPtr).

Function: matGetVariable to get the variable array with the specified name

`mxArray * matGetVariable(MATFile * pMF, const char * name);

   read the array value of the specified variable name from the MAT file, and an error occurs when 0 is returned.

Function: matGetVariableInfo to get the variable array with the specified name

  mxArray * matGetVariableInfo(MATFile *pMF, const char * name);

  read the array header of the variable with the specified name from the MAT file.
  the array header contains all the same information as the array, except that pr, pi, ir and jc data structures are not assigned to non recursive data types. That is, cells, structures, and objects contain pointers to other array headers, but the pr, pi, ir, or jc fields of numbers, strings, and sparse arrays do not contain valid data.
   the purpose of array header is to quickly access information about the array without reading all the actual data of the array. Therefore, functions such as mxGetM, mxGetN, and mxGetClassID can be used with array headers, but mxGetPr, mxGetPi, mxGetIr, mxGetJc, mxSetPr, mxSetPi, mxSetIr, and mxSetJc cannot. Array headers should not be returned to MATLAB (for example, through the MEX API) or any other non matrix access API functions that require a full mxArray (for example, engPutVariable(), matPutVariable(), and mexPutVariable()).
    note: this function is the same as matGetVariable, but it cannot get the entity through mxGetPtr).

*Function: mxGetM, get the number of rows

size_t mxGetM(const mxArray *pa);

*Function: mxGetN, get the number of columns

size_t mxGetN(const mxArray *pa); 

*Function: double * mxGetPr to obtain the entity pointer of mxArray

double *mxGetPr(const mxArray *pa);

   (PS: data storage is stored column by column)

*Function: matClose to close the mat file

matError matClose(MATFile *pMF);

  close the MAT file opened with matOpen. After matClose returns, the pointer to the MATfile parameter is invalid. Zero is returned for success and EOF is returned for error.


Mat file validation




bool MatlabManager::openMatFile(QString filePath)
    MATFile *pMATFile = 0;

    pMATFile = matOpen(filePath.toUtf8().data(), "r");
        LOG << "Failed to matOpen:" << filePath;
        QMessageBox::information(0, "Failed", QString("Failed to open file:%1").arg(filePath));
        return false;
//    QMessageBox::information(0, "Succeed", QString("Succeed to open file:%1").arg(filePath));

    char **namePtr = 0;

    // Get the first variable name and
    mxArray *pMxArray = matGetNextVariable(pMATFile, 0);
//    mxArray *pMxArray = matGetNextVariableInfo(pMATFile, 0);
        QMessageBox::information(0, "Failed", QString("Failed to matGetNextVariableInfo"));
        return false;
//    QMessageBox::information(0, "Succeed", QString("Succeed to matGetNextVariableInfo"));
    // Gets the number of rows and columns
    int rows = mxGetM(pMxArray);
    int cols = mxGetN(pMxArray);
//    QMessageBox::information(0, "Succeed", QString("Succeed to get row:%1 col:%2").arg(rows).arg(cols));
    LOG << "read rows:" << rows << ", cols:" << cols;
    // Get pointer address
    double *pData;
    pData = mxGetPr(pMxArray);
    // display
    for(int col = 0; col < cols; col++)
        QString str;
        for(int row = 0; row < rows; row++)
            str += QString("%1").arg((double)(pData[col * rows + row])) + ",";
        LOG << str;
    // Close file
    LOG << pMATFile;
    pMATFile = 0;
    LOG << pMATFile;

Demo demo

  read out value:

   it can be seen that the value is read vertically. The behavior here is 6 rows and 10 columns:
  the actual reading sequence is as follows:
  after optimization code correction:


Project template: v1.1.0

  testMatlabDemo_ Engineering formwork_ v1.1.0_ Read matlab file and print data.rar


Pit entry

Pit entry 1: enter the matlab main interface, and the pallet is not correct


   after installation, there is only one icon, as follows:
   open to enter the engineering interface (actually, it is only a small tool in matlab):




   go into the MATLAB installation directory bin and directly open matlab.exe. After reading the matlab main interface, it will not come out immediately. Wait a few more times (a few minutes). The following figure will come out. This is the main interface of MATLAB:

Pit 2: get mxArray, get its pointer, print downtime


  get mxGetPtr, get 0x00.


    when obtaining mxArray, the matGetNextVariableInfo function is used. In fact, the matGetNextVariableInfo function is required to obtain the value. The matGetNextVariableInfo function is to obtain the header information of non entities (which may improve the speed).



Tags: Qt

Posted on Wed, 10 Nov 2021 03:51:28 -0500 by DF7