How to use an overloaded function from a MATLAB compiled Dll in C# via P/Invoke ?

I apologize if this is a rather simple problem, but my experience with C# is limited and I didn't find a similar question searching.
Assuming I have some MATLAB function myfun.m with various possible inputs, i.e. as an example [A,B]=myfun(N,M,O), [A,B]=myfun(N,M,O,P) and [A,B]=myfun(N,M,O,P,Q). I have used mcc (and in this case Visual Studio's compiler) to create a cshared library, let's just call it myfun.dll and now I want to use the function myfun in specific ways, [A,B]=myfun(N,M,O) and [A,B]=myfun(N,M,O,P).
I assume it's not enough to simply use DllImport and then define two separate functions, i.e.* :
[DllImport(...., Entry Point ="myfun")]
public static ... myfun1(numargout,[Out] A, [Out] B, [In] N, [In] M, [In] O);
[DllImport(...., Entry Point ="myfun")]
public static ... myfun2(numargout,[Out] A, [Out] B, [In] N, [In] M, [In] O, [In] P);
*Leaving out the data types, which I would assume those are not of particular importance to our problem and the usual stuff, such as myfunInitialize and myfunTerminate.
Sorry if this has been answered already, thanks in advance for all help.

 Accepted Answer

I am assuming you use varargin in your definition of myfun.m, in order to allow a variable number of inputs? I don't know for sure, but it might be a good idea to look at the expected function prototypes in the generated header file. From what I can tell in the section 'Using varargin and varargout in a MATLAB Function Interface' on this page, you might need to pass a cell-array of input arguments where "varargin" is expected.

1 Comment

Sorry it took me so long to answer you. Indeed I could've passed a cell-array of input arguments for 'varargin', but in this case it was enough to manipulate the number of output arguments (the above variable numargout) for my purposes, i.e. specifying numargout=2 would cause the program to use the correct algorithm.
Thanks for the answer. Fortunately I was able to solve all my problems regarding this last week.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!