Duplicating an mxArray without first storing it as a plhs?

1 次查看(过去 30 天)
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.

采纳的回答

Philip Borghesani
编辑:Philip Borghesani 2017-1-9
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write 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