user2719731
user2719731

Reputation: 1

Matlab API reading .mat file from c++

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;
}

Upvotes: 0

Views: 433

Answers (0)

Related Questions