1

I want to access the properties of a custom MATLAB class in a MAT-file in a C++ standalone application. The custom class is a class created in MATLAB with something like this:

classdef customClass
    properties
        myProp
    end

    methods
        function obj = customClass(arg1,arg2)
           obj.myProp = arg1 + arg2
        end
    end
end

An instance of this class is now saved to a MAT-file and should be accessed by a standalone C application.

Apparently, MATLAB offers a library for reading MAT-files in C applications. This works fine for the "normal" types and the API seems to offer the function mxGetProperty for accessing custom objects. However, if I try to run a minimal example using this function, it fails with an empty assertion in management.cpp:671. The minimal example is:

#include <iostream>
#include "mat.h"

int main()
{
    MATFile* file = matOpen("test.mat", "r");
    if (file == nullptr)
        std::cout << "unable to open .mat" << std::endl;

    mxArray* customClass = matGetVariable(file, "c");
    if (customClass == nullptr)
        std::cout << "unable to open tcm" << std::endl;

    mxArray* prop = mxGetProperty(customClass, 0, "myProp");

    if (prop == nullptr)
        std::cout << "unable to access myProp";
}

A closer look to the documentation reveals the limitation: mxGetProperty is not supported for standalone applications, such as applications built with the MATLAB engine API.

Is there any other possibility to access a customClass in a MAT-file from a standalone C++ application?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Tritium
  • 23
  • 3

1 Answers1

1

classdef variables are opaque objects in MATLAB, and the details of how properties are stored in them is not published. You have to use official API functions to get at them (and mxGetProperty makes a deep copy btw). So you are stuck. My advice is to extract the properties you are interested in from the object and then save those to the mat file.

James Tursa
  • 2,242
  • 8
  • 9