Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import 3D array from MAT-file using C++

Tags:

c++

matlab

I would like to know if there is a way to know the 'z' dimension of a 3D array when reading data from a 'MAT-file' using the MATLAB API. I've implemented a function to load the data from file as follows:

double* importMATFile(const char* i_file)
{
    MATFile *pMF;
    // open MAT-file
    pMF = matOpen(i_file, "r");
    // check for file errors

    // Matlab Array Data
    mxArray *mArrayData;
    // Matlab Variable Name
    const char* mVarName = NULL;
    // read data from file
    mArrayData = matGetNextVariable(pMF, &mVarName);

    // pointer to mxArray data
    double *dataPtr; 
    dataPtr = (double*) mxGetPr(mArrayData);

    // NOTE MATLAB work in COLUMN-MAJOR order

    // dimension of the array : rows
    int32_t NROWS = mxGetM(mArrayData);
    // Right now the z dimension must be known a priori
    int32_t NDEPTH = 32
    // dimension of the array : cols
    int32_t NCOLS = mxGetN(mArrayData) / NDEPTH;

    return dataPtr;
}

I'm stuck when getting the DEPTH value, in order to know the number of columns. I've have noticed that the result of the function mxGetNumberOfDimensions(mArrayData) is 3, so, the API knows there are three dimensions.

like image 447
pQB Avatar asked Mar 15 '26 13:03

pQB


1 Answers

I believe what you want is mxGetDimensions. It will return the size of each of the dimensions. This should work for any number of dimensions, not just 3.

like image 89
Jonathan Avatar answered Mar 18 '26 03:03

Jonathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!