gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:52:21 25 4
gpt4 key购买 nike

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

classdef customClass
properties
myProp
end

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

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

显然,MATLAB 提供了一个 library用于在 C 应用程序中读取 MAT 文件。这适用于“正常”类型,API 似乎提供了函数 mxGetProperty用于访问自定义对象。但是,如果我尝试使用此函数运行一个最小示例,它会失败并在 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";
}

仔细查看文档会发现限制:mxGetProperty 不支持独立应用程序,例如使用 MATLAB 引擎 API 构建的应用程序。

是否有任何其他可能从独立的 C++ 应用程序访问 MAT 文件中的 customClass

最佳答案

classdef 变量在 MATLAB 中是不透明的对象,属性如何存储在其中的细节没有公布。您必须使用官方 API 函数来获取它们(顺便说一句,mxGetProperty 进行了深度复制)。所以你被困住了。我的建议是从对象中提取您感兴趣的属性,然后将它们保存到 mat 文件中。

关于c++ - 从 MAT 文件中读取 C 应用程序中的自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54451410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com