1

我有以下 Matlab“类设计”:

classdef foo
    properties
        a;
        b;
    end

    methods
        function obj = myFun(obj)
            obj.a = 42; // some calculation takes place here
        end
    end
end

classdef bar
    properties
        foos; // this is going to be an array of foos
        otherStuff;
    end

    methods
        function obj = someFun(obj)
            for i = 1:length(foos)
                obj.foos(i) = obj.foos(i).myFun;
            end
        end
    end
end

正如我所写的,一个对象将具有一组其他对象作为其类属性。

我正在尝试将其重写为 C 代码并将其与 MEX 一起使用。我现在的问题是:如何访问数组 foos 的不同元素?我知道 API 函数 mxGetProperty。使用此函数,我可以访问每个数组条目的属性(例如 obj.foos(i).a),但不能访问整个对象(我想要 obj.foos(i) )。但是,我需要整个对象(作为 mxArray *)才能调用成员函数。

任何想法如何做到这一点?

谢谢

4

1 回答 1

0

在调用 mxGetProperty 以获取对象数组后,最好调用 MATLAB 对该对象数组进行索引。如果 bar_mx 是 bar 对象的 mxArray 则

 foos_prop = mxGetProperty(bar_mx, 0, "foos"); // Gives foos property
 foos_i = mexCallMATLAB( ... [foos_prop, i]) call MATLAB function to get i'th foo

您可以结合调用来获取第 i 个对象以及对该对象的方法调用。如果您想在 C++ 中进行索引,您可能需要调用subsref函数。mexCallMATLAB 比为 subsref 创建 struct 参数更容易。

于 2013-09-13T17:42:48.193 回答