mahNNUser
mahNNUser

Reputation: 1

How to read a 'CompactSVMModel' save in a .mat file from a C++/Qt program

I tried to save a machine learning model into a .mat file, and then read it from a Qt/C++ application. Following the documentation, I managed to read files containing basic types like double or int. As my model is of type 'classreg.learning.classif.CompactClassificationSVM', I wonder if it is still possible to read it.

As and example, i saved this iosnosphere model on matlab

    load ionosphere
    SVMModel = fitcsvm(X,Y,'Standardize',true,'ClassNames',{'b','g'})
    CompactSVMModel = compact(SVMModel)
    save("CompactSVMModel.mat", "CompactSVMModel")

I imported the relevant library in the CMakeLists.txt

    find_package(Matlab "x.xx" EXACT COMPONENTS  "MX_LIBRARY" "MAT_LIBRARY" "DATAARRAY_LIBRARY" )

    target_link_libraries(target PRIVATE Matlab::mat )

I used the "mat.h" library to open and read the data.

    #include "mat.h"


    void readMatFile(const std::string& filepath) {
        std::string stdFilename = filepath + "/CompactSVMModel.mat";
        const char *variableName = "CompactSVMModel";

        // Open the MAT-File
        MATFile *matFile = matOpen(stdFilename.c_str(), "r");
        if (!matFile) {
            qDebug() << "Error opening MAT file: " << stdFilename;
        }

        // Read the variable from the MAT-File
        mxArray *dataArray = matGetVariable(matFile, variableName);
        if (!dataArray) {
            qDebug() << "Error reading variable: " << variableName << " from MAT file: " << stdFilename;
        }
        ...
    }

At the if statement, matFile is NULL.

Upvotes: 0

Views: 34

Answers (0)

Related Questions