Duplicating an mxArray without first storing it as a plhs?

1 回表示 (過去 30 日間)
Petter Stefansson
Petter Stefansson 2017 年 1 月 9 日
コメント済み: Petter Stefansson 2017 年 1 月 9 日
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 日
編集済み: 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 Center および File ExchangeWrite 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