Use mexCallMATLAB inside of a C-Function only passing pointers

1 view (last 30 days)
Hello everyone,
inside of a mexFunction, I want (i. e. my specifications are) to call a C-function myfcn(), and inside of myfcn() I want to use mexCallMATLAB.
The arguments passed to myfcn all are pointers, like
myfcn(double *ptr1, double *ptr2, int *ptr3, ...)
and so on. As I unterstood, to use mexCallMATLAB I have to store the necessary information in variables of type mxArray.
My question now is, how can I set up the mxArrays I need as in
array1 = mxCreateDoubleArray(m, n, mxREAL);
memcpy(array1, ptr1, sizeof(double) * m * n);
since from my specifications it is neither possible to pass additional parameters for m and n to myfcn(), nor to pass arguments different from pointers which do not allow me to determine the size of the arrays they're pointing to.
Is there another possibility of solving this problem than e. g. extending the array and storing their length in additional fields (which is what I not really want to do because the structure of the data in fact is fixed, too), but maybe something like using mexCallMatlab with different inputs, or setting up the mxArray in some other clever way?
Thank you very much for your support!
Adrian Buerger
  2 Comments
Titus Edelhofer
Titus Edelhofer on 8 Aug 2013
Hi Adrian,
if at all this question can be answered, then only if you "know" inside your myfcn the size of ptr1[] etc. The code in myfcn, how does it know the array bounds? It somehow needs to know, either by passing the size, or as constants or what so ever. Just looping over the array until you get a memory failure is probably not the best way ;-).
Titus
Adrian Buerger
Adrian Buerger on 8 Aug 2013
Hello Titus,
yes, this might not be the best way :-) as I already mentioned, I will reconsider my specifications if there's just no possibility to do it this way.
Thank you for your answer!

Sign in to comment.

Accepted Answer

Jan
Jan on 8 Aug 2013
This sounds magic. When your function obtains pointers as inputs without specifying the dimensions of the arrays, how can the function determine the length of the arrays?
This cannot work:
array1 = mxCreateDoubleArray(m, n, mxREAL);
% memcpy(array1, ptr1, sizeof(double) * m * n); ERROR!
Better:
memcpy(mxGetPr(array1), ptr1, sizeof(double) * m * n);
  1 Comment
Adrian Buerger
Adrian Buerger on 8 Aug 2013
Hello Jan,
your question points out what I have also questioned myself already.
Well ok, if there's really no chance to do it this way, I'll have to reconsider my specifications. I do not think this problem was in mind when those were made :-)
With the code you are right of course, I just made a typo here.
Thank you for your answer!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!