Use mexCallMATLAB inside of a C-Function only passing pointers

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

Titus Edelhofer
Titus Edelhofer 2013 年 8 月 8 日
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 2013 年 8 月 8 日
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!

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

 採用された回答

Jan
Jan 2013 年 8 月 8 日

1 投票

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

Adrian Buerger
Adrian Buerger 2013 年 8 月 8 日
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!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by