I'm writing an application which need to have some things done by the matlab engine. However, inside the matlab scripts I need some callback functions. Is it possible to create a callback function in a matlab script back to a c++ function?
Asked
Active
Viewed 672 times
3
-
If the c++ callback function can get all the data it needs as input arguments, you could just create a mex file and call it from the matlab function that the c++ code invoked. Or is your use case more complex? – Itamar Katz Dec 08 '11 at 13:21
-
Ha, I like that. Can't say I've ever called a mex function from an engine function call. You should probably try to avoid calling any new engine functions from within the mex, else the universe could unravel. As Itamar points out, your mex "callbacks" obviously won't have access to the storage of the calling function. If you need this, your engine function will probably have to return and be recalled. – Jonathan Dec 08 '11 at 15:02
-
For a project for school we have to create an application, in our case a game in c++, which should be controlled with hand gestures. However, the hand gesture recognition part is done in MatLab (a requirement), and this is more or less a separate application. Currently I'm writing the data which needs to be send to c++ to a queue in MatLab, and the c++ application just checks every update cycle if there are any elements in the queue, but I was hoping that there was a more elegant solution. – Tiddo Dec 10 '11 at 12:11
-
Can you provide some more details? What is that C++ code that you want to run from Matlab? What is the purpose of it? – Andrey Rubshtein Dec 13 '11 at 14:46
-
Matlab should let the C++ application know that an event has occurred. As I said before, our application is a game with hand-gesture control. So instead of using keyboard and mouse events, we need to capture and process webcam data. This part is done in MatLab. However, MatLab should let the application know that it has recognized the hand gesture and which action the application should perform. Preferably, I'd like MatLab to call a C++ function which can change some variables for the game (e.g. update the speed of the character). – Tiddo Dec 14 '11 at 00:51
-
@Tiddo: What windowing system? I'd use a MEX function to post a custom window message, which the C++ game loop will process just like it processes keypresses, mouse input, window resize events, etc. – Ben Voigt Feb 03 '12 at 20:38
1 Answers
1
Disclaimer : I never tested this approach by myself
You can do it by compiling your Matlab code as COM component using Matlab builder NE.
This component will be sending COM events, and your application will subscribe to them.
See the "Adding Events to COM Objects" section in Matlab Builder NE documentation.
You can read about COM here:
http://en.wikipedia.org/wiki/Component_Object_Model

Andrey Rubshtein
- 20,795
- 11
- 69
- 104
-
Thanks for your answer. I was already afraid that this would be the only way, and I don't have experience with COM and I also don't really have the time to learn about COM. But I'll look into it when I have the time! – Tiddo Dec 22 '11 at 09:42
-
@Tiddo, if you consider to write your main application in C#, COM will be painless. – Andrey Rubshtein Dec 22 '11 at 09:44
-
The appliction is a slight modification of an application we've already written in C++. This application is for a course Computer Science and our main focus should be on the image recognition part, so I don't think it's worth our time to rewrite the entire application. However, I will keep this in mind for my next project! – Tiddo Dec 22 '11 at 09:48