C++ 从MAT文件读取C应用程序中的自定义类

C++ 从MAT文件读取C应用程序中的自定义类,c++,matlab,matlab-class,C++,Matlab,Matlab Class,我想访问C++独立应用程序中的一个Matlab文件中自定义Matlab类的属性。自定义类是在MATLAB中创建的一个类,如下所示: classdef customClass properties myProp end methods function obj = customClass(arg1,arg2) obj.myProp = arg1 + arg2 end end end 此类的一

我想访问C++独立应用程序中的一个Matlab文件中自定义Matlab类的属性。自定义类是在MATLAB中创建的一个类,如下所示:

classdef customClass
    properties
        myProp
    end

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

此类的一个实例现在保存到MAT文件中,应由独立的C应用程序访问

显然,MATLAB提供了在C应用程序中读取MAT文件的方法。 这适用于“普通”类型,API似乎提供了访问自定义对象的功能。但是,如果我尝试使用此函数运行一个最小的示例,它将失败,并在
management.cpp:671
中出现一个空断言。最简单的例子是:

#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";
}
#包括
#包括“材料h”
int main()
{
MATFile*file=matOpen(“test.mat”、“r”);
if(file==nullptr)

std::coutclassdef变量在MATLAB中是不透明的对象,属性如何存储在其中的细节尚未公布。您必须使用官方API函数来获取它们(顺便说一句,mxGetProperty制作了一个深度副本)我的建议是从对象中提取您感兴趣的属性,然后将它们保存到mat文件中