How do I use C++ classes in a mex file?

Dear All,
I have a several header files and corresponding c++ files for classes that I have written for a C++ program. I would like to call methods in these classes from a mex file.
So, I have the C++ file that contains my mexFunction. In that file, I would like to instantiate an object from a class in a separate c++ file. How can I do that? Do I have to somehow "mex" the file that contains the separate class?
Thank you.

Respuestas (2)

Kaustubha Govind
Kaustubha Govind el 2 de Mzo. de 2012
You can instantiate it just like you would in any other C++ file. And yes, you will need to compile that class also into the MEX file. You can simply add the name of the .cpp file to the end of the MEX command.
>> mex mymexfile.cpp otherclass.cpp

1 comentario

Mohamed Abdelkader Zahana
Mohamed Abdelkader Zahana el 3 de Sept. de 2015
Thanks Kaustubha Govind . I tried your suggestion on MATLAB R2014b, Windows 8 64bit and it works!. Just wanted to mention that, so pther people facing the problem could benefit from it.

Iniciar sesión para comentar.

Ken Atwell
Ken Atwell el 3 de Mzo. de 2012

0 votos

While you can compile C++ code into a MEX file (see above), accessing methods will take a bit of work. You will still need to create a mexFunction API so MATLAB has something to call. You don't have any direct way of invoking C++ methods from MATLAB -- you might need to implement several mexFunction files to "wrap" the methods you need to call.
If that sounds like too much work and you are on Windows, you may want to look into adapting your C++ class into a .NET assembly -- unlike natively compiled C++, MATLAB can instantiate .NET classes and invoke methods.

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Mzo. de 2012

Comentada:

el 3 de Sept. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by