I have a C function created using Compiler CDK and it's returning a large array but it's MxArray** type. How do I access all the data retunred to the calling C function?

2 views (last 30 days)
Here's the C calling function from main().
#include <stdio.h>
#include "libMPrime_CA_P.h"
#include "matrix.h"
int main(void)
{
libMPrime_CA_PInitialize();
mxArray *mydata;
int numargs=1;
int rtn;
rtn = mlfMPrime_CA_P(numargs,&mydata); // Function that was created using Compiler CDK returns MxArray**
printf("Function Completed rtn:%0d with numargs =%d\n",rtn,numargs);
libMPrime_CA_PTerminate();
return 0;
}
//Here's the funcrion header in the library.
extern LIB_libMPrime_CA_P_C_API bool MW_CALL_CONV mlfMPrime_CA_P(int nargout, mxArray** a);
Here's the function header written in MATLAB
function [a] = MPrime_CA_P
a is 1 x 127689 double values
....
What does nargout do and how do I access all the data in MxArray** a?

Answers (1)

James Tursa
James Tursa on 22 May 2020
Edited: James Tursa on 22 May 2020
A function signature like that typically means that the mlfMPrime_CA_P( ) function is creating an mxArray and then returning the address of that mxArray in the variable mydata. That is, mydata is intended to be an output of the function call. So you can use mydata like an ordinary mxArray pointer and use normal API function on it in the calling function. E.g., something like:
mxArray *mydata = NULL; // You should initialize it in case the function doesn't set it
:
rtn = mlfMPrime_CA_P(numargs,&mydata);
if( mydata != NULL ) {
printf("Function returned an mxArray of class %s\n",mxGetClassName(mydata));
// Other API functions to manipulate mydata go here
mxDestroyArray(mydata);
}
  17 Comments
James Tursa
James Tursa on 28 May 2020
Edited: James Tursa on 28 May 2020
I'm still not making myself clear. Here is what I would expect the C-code to look like:
mxArray *dataval = NULL;
:
rtn = mlfMPrime_CA_P(numargs,&dataval);
if( dataval ) {
printf("Function returned an mxArray, ndim = %d\n",mxGetNumberOfDimensions(dataval));
}
Do your data types and calling sequence look like the above?
George Nicholas
George Nicholas on 28 May 2020
I had:
printf("Function returned an mxArray, ndim = %d\n",mxGetNumberOfDimensions(&dataval));
Changed to:
printf("Function returned an mxArray, ndim = %d\n",mxGetNumberOfDimensions(dataval));
Now error is:
/bin/ld: /tmp/ccKZSl27.o: undefined reference to symbol 'mxGetNumberOfDimensions_800_proxy'
/opt/tools/matlab_r2019b/runtime/glnxa64/libmwmclmcrrt.so.9.7: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!