Chris
Chris

Reputation: 2060

Access Matlab classes in MEX/C-code

I have to rewrite some matlab code into C which will then be embedded into Matlab using MEX once again. So far, I've read some tutorials and examples in how this works for simple data structures. (I've never done that before, even though I would consider myself experienced in both Matlab and C).

So here is the problem:

I have given something like that

classdef MyClass
     properties
          foo;
          bar;
          blub;
          somethingElse;
     end

     methods

          function obj = myFun(obj) % really just some random example code
               obj.foo = obj.bar;
               obj.blub = 42;
               for i = 1:length(obj.somethingElse)
                    obj.somethingElse(i) = i*i;
               end;
          end
     end
end

I want to rewrite myFun as a MEX/C-function. If I pass a class to a MEX-function, how can I access the different properties of this class?

Thanks

Upvotes: 6

Views: 1351

Answers (1)

Amro
Amro

Reputation: 124563

You have the following functions in the MEX API:

mxGetProperty and mxSetProperty

Their use is equivalent to:

value = pa[index].propname;

pa[index].propname = value;

Note that these functions create deep copies of the data. There are undocumented functions to work with shared data.

Upvotes: 11

Related Questions