How can I use the MEX function signature in a DLL library called using loadlibrary and calllib?

1 view (last 30 days)
I need to build a component of my statistical model using C. It will involve hundreds of functions called through Matlab. Consequently, I don't want to build a MEX interface for each such callable function. A more main-tenable approach would be to build a single Matlab specific DLL interface, which calls into the C subsystem. The functions will take and return mxArray's, rather than C-specific structures. If I have a function using a mex-type function signature in the DLL [like
void my_function(int nlhs, const mxArray* plhs[], int rhs, const mxArray* prhs[])]
it turns out that it behave exactly like a mex function, differing only in the call mechanism (using the calllib function). However, this feature isn't documented anywhere in the shared library documentation. Is this supposed to be the correct use of the mex-type input/output signature through calllib? If yes, then I can have all required functions in a single DLL, rather than having 100s of different mex files.
What other mechanisms do I have to return multiple mxArray outputs from a single DLL function called using calllib? Eg, if i have a function signature -
mxArray* process(mxArray* in1, mxArray* in2)
- called using calllib, I can send 2 inputs and get one output. In mex, returning multiple outputs is easy through the prhs[] array. How can I do the same in a calllib function?

Accepted Answer

Philip Borghesani
Philip Borghesani on 15 Nov 2013
Calling a function with a mex signature with calllib is supported. The example yprime.c in extern\examples\shrlib is used demonstrate this. Documentation is minimal because we have seen little demand for this and have had few questions about it.
Other options for returning multiple things from a library call are to return an mxArray containing a cell array or structure of values or to use c pointer types and a standard C style function possibly with mxArray * arguments too. Modifying mxArray * input arguments is not recommended. Remember that the the function int foo(int size, int *vec) can be called with calllib like:
[status, outvec]=calllib('lib','foo',10,1:10);
  2 Comments
Ankur
Ankur on 15 Nov 2013
If I want to create a function to be called using calllib, that returns two double matrices as output and accepts two double matrices as input, what should be the function signature? I tried the following, but it doesn't work:
mxArray* process(mxArray** out2, mxArray* in1, mxArray* in2) {
// Example code simplified
mxArray *x, *y;
x = some_processing_on_inputs(in1,in2);
y = other_processing_on_inputs(in1,in2);
*out2=y;
return x;
}
With mex, it's much simpler using nlhs[] and nrhs[] arrays, but I don't want to have 100's of mex files for 100's of functions. Would rather prefer calllib.
Philip Borghesani
Philip Borghesani on 15 Nov 2013
Did you miss my first statement? Go ahead and use mex style functions of the form:
void my_function(int nlhs, const mxArray* plhs[], int rhs, const mxArray* prhs[])
This allows any number of inputs and outputs along with varargin and varargout.

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!