Is it possible to pass pointers to MATLAB structures through a COM interface?

5 次查看(过去 30 天)
I generate a few classifiers in MATLAB through a COM interface. I would like to pass a pointer to them, the MATLAB classifiers, back through the COM interface, so that I can use them later if necessary (say, to call Predict on data I'm streaming from somewhere) I don't need to do anything to the classifier in my code, I just want to hang onto it and use it after training and other operations have been accomplished.
So to be clear, I don't need to do anything to the pointer except hang on to it in my code, and pass it back to MATLAB to tell it which structure it should use.
Is this possible?
  6 个评论
Guillaume
Guillaume 2016-2-3
My understanding is that the code is written in C# and that the COM interface is automatically generated by .Net. I don't think there's any C/C++ code involved.
Regardless, I'm also confused as to which way the structure is traveling.
Isaac Sherman
Isaac Sherman 2016-2-3
编辑:Isaac Sherman 2016-2-3
It's C# code. I don't do anything at all with the structure except to pass it along as a model to the predict function, so I do need it to remain in memory, and I need a way in C# to direct MATLAB to the proper classifier. So if I have a bunch of classifiers to choose from, I'd like to use predict to get their classifications without retraining them every time I want to make a prediction. Predict is documented here: http://www.mathworks.com/help/stats/compactclassificationdiscriminant.predict.html

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2016-2-3
It's been a long time since I've written any COM interfaces but I'm fairly certain that passing raw pointers around is completely antithetical to the COM architecture. For a start, the COM interface may not even be in the same process or even on the same machine. I don't think the COM IDL (interface definition language) allows for specifying raw pointers as method parameters.
And of course, if you're dealing with .Net, your pointer is managed and you can't just pass it around.
It's not clear from your question what the pointer points to. Is it another COM interface or just a plain structure? If it's a COM interface, you'd just pass the interface, if it's a structure, you'd pass an opaque handle guaranteed to be unique for each structure. It could simply be the pointer converted to uint64, or it could be an index into an array of pointers.
In any case, you also have to make sure that when you receive this opaque handle back from matlab, it is still valid.
  7 个评论
Guillaume
Guillaume 2016-2-4
Yes, there are FEX contributions but I wouldn't trust them with objects. Matlab simply does not expose the required machinery to safely serialise/deserialise objects (except maybe through mex?).
In particular, it's not possible to deserialise an object with immutable properties. MathWorks have explicitly told me that they are not going to change that.
James Tursa
James Tursa 2016-2-4
编辑:James Tursa 2016-2-4
FYI, yes there are mex functions for this. But they are undocumented and as such can't be fully trusted to work as expected, especially with classdef OOP objects. The internal storage details of classdef OOP objects is not published, so there would be no easy way to test these functions for such objects.

请先登录,再进行评论。

更多回答(1 个)

James Tursa
James Tursa 2016-2-3
Not sure how you have your particular COM interface set up. But, e.g. the MATLAB Engine API connection from a C/C++ program to MATLAB is through a COM interface. In this case, these are separate processes that interact through this COM interface, and as such have separate memory. So any pointer that was passed from one process would be invalid to use in the other process. I.e., if a MATLAB object was created in the Engine and a pointer to it was passed back through the COM interface to the C/C++ program, you couldn't use the pointer in your C/C++ program because the memory behind the pointer does not belong to your C/C++ process ... it belongs to the MATLAB Engine process. The pointer is invalid as far as your C/C++ program is concerned. Attempting to use it would likely crash your C/C++ program.
  2 个评论
Isaac Sherman
Isaac Sherman 2016-2-3
I want to hang on to the pointer to pass it back to MATLAB later. I'll edit the question for clarity, but let's say I train 3 classifiers. Turns out the 2nd worked the best- and I want to feed it data to make predictions on by using the com interface, passing C++ data to the classifier in MATLAB. My C++ knows which of the 3 classifiers to use, nothing else about the underlying structure of the data, so I would like to pass a pointer back to MATLAB to tell it which classifier it should use.
Walter Roberson
Walter Roberson 2016-2-3
If the MATLAB session has not been closed, pass back an index number, which you can use to index a struct or cell array of data or cell array of function handles.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write COM Applications to Work with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by