MATLAB: MxUnshareArray doesn’t seem to work

memorymexperformanceundocumented

Hi,
I'm trying to write a very simple function to learn how to write mex in place function.
I wrote the following function:
#include <matrix.h>
#include <mex.h>
void mexFunction(int nargout, mxArray *argout[], int nargin, const mxArray *argin[]) {
const int res = mxUnshareArray(const_cast<mxArray *>(argin[0]), true);
argout[0] = mxCreateSharedDataCopy(argin[0]);
double *data = (double *)mxGetData(argin[0]);
data[0] =res;
}
I ran it:
x = ones(1e8,1) * 10;
x = fun(x);
And I still see that the function copies x. I looked at the call time of this function and the same function without the mxUnshareArray. With the mxUnshareArray it takes much longer (due to the coping of the data).

Best Answer

I found a solution using the new Matlab::data api.
I'm posting it on my other post. Please check it on: