preface

Introduced the basic environment, the final is to read display. Mat file, this read mat file and display.


supplement

The mat file tested is of type double.

Matlab library data type

Variable type: matError, error variable

Error type, which is actually an integer.

Variable types: MATFile, mat file pointer

Open the pointer returned by mat file, operation file needs to be used until closed.

Variable type: mxArray, MAT array

The 2-d array type generated by opening the MAT file.


Matlab library function open to read the need to use

There are actually some functions, but this article only lists the functions that need to be used in this article (marked with “*”).

* Function: matOpen, open the MAT file

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

Use “Mode” to open MAT file “filename”. 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” – Updates files to allow reading and writing without deleting existing files

Function: matGetDir to get a list of group names

char ** matGetDir(MATFile * pMF, int *num);
Copy the code

Gets a list of array names in the MAT file. The array of strings 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, get the next variable array

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

Reads the next array value from the current file location of the MAT file pMF. This feature can only be used with matOpen and matClose. Passing pMF to any other API function will cause matGetNextVariable() to not work properly. Special note: polling the variable table, then pass in 0 as the second argument.

Function: matGetNextVariableInfo, get the next variable array

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

Reads the array header of the next array value in the MAT file. This feature 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 efficient use of array headers. Note: This function is the same as matGetNextVariable, but you can’t get the entity via mxGetPtr.

The matGetVariable function gets an array of variables with the specified name

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

Error reading array value of specified variable name from MAT file, return 0.

The matGetVariableInfo function retrieves an array of variables with the specified name

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

Reads the array header of the variable with the specified name from a MAT file. The array header contains all the same information as the array, except that the 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 numbers, strings, and sparse arrays contain no valid data in the PR, PI, IR, or JC fields. The purpose of the array header is to quickly access information about an array without reading all of its actual data. 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 you can’t get the entity via mxGetPtr.

* Function: mxGetM, get the number of lines

size_t mxGetM(const mxArray *pa);
Copy the code

* Function: mxGetN, get the number of columns

size_t mxGetN(const mxArray *pa); 
Copy the code

* Double * mxGetPr to get the entity pointer to the mxArray

double *mxGetPr(const mxArray *pa);
Copy the code

(PS: Data store is stored column by column)

* Function: matClose, close the MAT file

matError matClose(MATFile *pMF);
Copy the code

Close the MAT file opened with matOpen. After matClose returns, the pointer to the MATfile parameter is invalid. Returns zero on success, EOF on error.


Mat file Verification

  


Demo

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

    pMATFile = matOpen(filePath.toUtf8().data(), "r");
    if(! pMATFile) { 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);
    LOG;
    if(! pMxArray) { QMessageBox::information(0."Failed".QString("Failed to matGetNextVariableInfo"));
        return false;
    }
// QMessageBox::information(0, "Succeed", QString("Succeed to matGetNextVariableInfo"));
    LOG;
    // Get the 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 the pointer address
    double *pData;
    pData = mxGetPr(pMxArray);
    / / show
    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;
    }
    LOG;
    // Close the file
    matClose(pMATFile);
    LOG << pMATFile;
    pMATFile = 0;
    LOG << pMATFile;
}
Copy the code


The Demo presentation

        Read fetch value:

As you can see, the value is read vertically, and the behavior here is 6 rows and 10 columns:The actual read sequence is as follows:After optimized code correction:


Project template: V1.1.0

TestMatlabDemo_ Project template _v1.1.0_ Read MATLAB files and print data.rar


Into the pit

Pit 1: The goods are not correct when entering the main interface of MATLAB

The problem

After the installation is complete, there is only one icon, as follows:Open it to enter the engineering interface (actually just a small tool in MATLAB) :

why

The unknown

To solve

Go into the MATLAB installation directory bin, directly open matlab.exe, read the MATLAB main interface, will not immediately come out, several times, etc. (a few minutes), will come out as follows, this is the matlab main interface:

Pit 2: Get mxArray, get its pointer print down

The problem

Get mxGetPtr get 0x00.

why

To get the mxArray, use the matGetNextVariableInfo function. To actually get the value, use the matGetNextVariable function. MatGetNextVariableInfo gets the non-entity header information (which may be faster).

To solve

  


Matlab+Qt development notes (a) : Matlab build Qt development matlib environment and Demo test “next article: please look forward to…