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

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 件のコメント

Isaac Sherman
Isaac Sherman 2016 年 2 月 3 日
Also, I happen to be using the C#/.NET COM interface.
James Tursa
James Tursa 2016 年 2 月 3 日
I am not familiar with this interface. What exactly are you allowed to pass back & forth?
Isaac Sherman
Isaac Sherman 2016 年 2 月 3 日
You can pass any of the fundamental types, except structures. Details are here: http://www.mathworks.com/help/matlab/matlab_external/handling-com-data-in-matlab-software.html
James Tursa
James Tursa 2016 年 2 月 3 日
Just to clarify, are you trying to examine the contents of these classifier structs inside the C/C++ code to determine which one is "best"? And then communicate that back to MATLAB?
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 日

1 投票

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 件のコメント

Isaac Sherman
Isaac Sherman 2016 年 2 月 3 日
It points to a classifier, which is a MATLAB struct.
Guillaume
Guillaume 2016 年 2 月 3 日
I'm confused now, are you creating matlab structures in the COM interface, so you're trying to pass that structure back to matlab? Or you want to pass a matlab structure to the COM interface?
By the way, if you're developing the code in .Net, there is no need to go through COM, matlab can talk directly to .Net code.
But either way, matlab structures are very different from .Net/C/COM structures. Even if you could pass a pointer to the structure between the two, you would have a hard time accessing it from the .Net/COM side.
Isaac Sherman
Isaac Sherman 2016 年 2 月 3 日
I am creating the structures in MATLAB (on the other side of the COM interface) and ideally, I would pass them back to C#/.NET, where they would reside until I wanted to call predict or some other function that needs them as an argument. They don't get modified, I just need to pass them to the predict functions. But I'm curious about how MATLAB communicates directly with COM- do you happen to have a reference handy?
Note that the classifiers are objects, not structures. You certainly can't pass a pointer to the object to C# or COM because matlab does not give you access to it (and even if you could, you'd be fighting against matlab memory manager).
The 'normal' way to pass opaque objects to another program so they can be reused later is to serialise / deserialise the objects. Unfortunately, matlab is very limited here, you can only serialise to a mat file. I guess you could save the classifiers in a mat file, and pass the binary content of the mat file to .Net, and when you need them back, write that content back to a mat file and read that file. That's very cumbersome.
A simpler setup, assuming you don't need persistence outside of the current matlab section is to store the classifiers in a cell array and pass the indices of the classifiers in the cell array to .Net. The .Net code later on returns the select classifier index, and on the matlab side, you just get the classifier at the given index from the cell array.
Pseudo-code
%create classifier, and pass to .Net:
%classifier1, classifier2, classifier3 are classifier objects
classifiers = {classifier1, classifier2, classifier3}; %cell array of classifier objects
netcode.SetClassifiers(1:numel(classifiers), somethingelse)
%.Net code simply store an integer for each classifier
%...
%later on retrieve chosen classifier
classifieridx = netcode.GetPreferredClassifier
chosenclassifier = classifiers{classifierdidx};
Walter Roberson
Walter Roberson 2016 年 2 月 3 日
There are File Exchange contributions to serialize to memory.
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 日

1 投票

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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeWrite COM Applications to Work with MATLAB についてさらに検索

製品

タグ

質問済み:

2016 年 2 月 2 日

編集済み:

2016 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by