Duplicating an mxArray without first storing it as a plhs?

4 views (last 30 days)
I’m writing a MEX file in C where I need to duplicate an mxArray in the code. What I’m currently doing is this:
plhs[0] = mxCreateDoubleMatrix(n, p, mxREAL);
C = mxGetPr(plhs[0]);
in = mxDuplicateArray(plhs[0]);
a = mxGetPr(in);
I’m creating a double mxArray and storing it as the first output to Matlab: plhs[0]. I then get the pointer to it called C and perform my calculations on that. A bit later in the code I need to make a copy of C, which works great by calling mxDuplicateArray on the plhs[0].
However, I would like to do the same thing but avoid placing C as a Matlab output ( plhs).
So instead I would like to be able to do something like this:
C = mxGetPr(mxCreateDoubleMatrix(n, p, mxREAL));
in = mxDuplicateArray(C);
a = mxGetPr(in);
But doing that does not work and makes Matlab crash. Can anyone advise me on how I should write it instead? I’m sure there is a very trivial solution to my problem, I’m just very unfamiliar with how to use the MEX functions.

Accepted Answer

Philip Borghesani
Philip Borghesani on 9 Jan 2017
Edited: Philip Borghesani on 9 Jan 2017
You don't need to put the value into plhs but you do need to put it somewhere. You must need the array for some reason later in the code too?
mxarray* ar1
mxarray* in;
double * C;
double * a;
ar1=mxCreateDoubleMatrix(n, p, mxREAL);
in = mxDuplicateArray(ar1);
C = mxGetPr(ar1);
a = mxGetPr(in);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!