0

I am trying to import .mat file for the algorithm developed in C++. I have imported the mat.h and all the related library from "matlabroot\extern\include". I am trying to run the following code but getting "undefined reference failure".

Error: 
FAILED: untitled.exe 
D:\Softwares\CLion\CLion 2022.1.1\bin\mingw\bin/ld.exe: CMakeFiles/untitled.dir/main.cpp.obj: in function `diagnose(char const*)':
C:/Users/rahul/CLionProjects/untitled/main.cpp:40: **undefined reference to `matOpen_800'**
D:\Softwares\CLion\CLion 2022.1.1\bin\mingw\bin/ld.exe: C:/Users/rahul/CLionProjects/untitled/main.cpp:49: **undefined reference to `matGetDir_800'**
#include "mat.h"
#include <iostream>
#include <vector>

void matread(const char *file, std::vector<double>& v)
{
    // open MAT-file
    MATFile *pmat = matOpen(file, "r");
    if (pmat == NULL) return;

    // extract the specified variable
    mxArray *arr = matGetVariable(pmat, "LocalDouble");
    if (arr != NULL && mxIsDouble(arr) && !mxIsEmpty(arr)) {
        // copy data
        mwSize num = mxGetNumberOfElements(arr);
        double *pr = mxGetPr(arr);
        if (pr != NULL) {
            v.reserve(num); //is faster than resize :-)
            v.assign(pr, pr+num);
        }
    }

    // cleanup
    mxDestroyArray(arr);
    matClose(pmat);
}

int main()
{
    std::vector<double> v;
    matread("data.mat", v);
    for (size_t i=0; i<v.size(); ++i)
        std::cout << v[i] << std::endl;
    return 0;
}
rinkert
  • 6,593
  • 2
  • 12
  • 31
  • It looks like you are still missing a library. Which libraries did you include in the build? Also, in your code you should also check for !mxIsSparse(arr) and !mxIsComplex(arr). Note that mxGetPr( ) is deprecated for later versions of MATLAB. You might want to use (double *)mxGetData(arr) instead. – James Tursa May 19 '22 at 17:16
  • `matOpen` and `matGetDir` are part of the `libmat` library, located at `matlabroot\extern\lib\win64\mingw64\libmat.lib`, did you include that library? – rinkert May 20 '22 at 07:01

0 Answers0