Duplicating an mxArray without first storing it as a plhs?

1 vue (au cours des 30 derniers jours)
Petter Stefansson
Petter Stefansson le 9 Jan 2017
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.
Thanks.

Réponse acceptée

Philip Borghesani
Philip Borghesani le 9 Jan 2017
Modifié(e) : Philip Borghesani le 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);

Plus de réponses (0)

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by