Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Matlab classes in MEX/C-code

Tags:

c

oop

matlab

mex

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

like image 891
Chris Avatar asked Aug 07 '13 14:08

Chris


People also ask

What is C MEX MATLAB?

A MEX file is a function, created in MATLAB®, that calls a C/C++ program. A MEX file is a function, created in MATLAB, that calls a C/C++ program or a Fortran subroutine. A MEX function behaves just like a MATLAB script or function.

How do I run a MEX code in MATLAB?

To call a MEX file, put the file on your MATLAB® path. Then type the name of the file, without the file extension. If you have MEX file source code, see Build C MEX Function for information about creating the executable function.

How do I call a function in C in MATLAB?

From within your MATLAB® code, you can directly call external C/C++ code, also called custom code or legacy code. To call C/C++ functions, use coder. ceval .

How do I debug a MEX file in MATLAB?

To debug your MEX functions, use the disp function to inspect the contents of your MEX function variables. You cannot use save to debug MEX function variables because code generation does not support it. Code generation does not support declaration of save as extrinsic.


1 Answers

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.

like image 196
Amro Avatar answered Sep 19 '22 10:09

Amro