mexFunction : mxDuplicateArray

≯℡__Kan透↙ 提交于 2019-12-13 22:24:03

问题


When I used mxDuplicateArray function, I met this error:

cannot convert double* to const mxArray* {aka const mxArray_tag*} for argument 1 to mxArray* mxDuplicateArray(const mxArray*).

Is there anyone who knows how to fix it?

This is part of my code:

    vector<int> *NNLt;
    double *NNLtout;
    Vector *V;
    Vector *Fb;
    mwSize *sn;
    mwSize nsn; 
    mwSize nf; 
    double hs;
    double bw;
    double mw; 
    mwSize ncols; 
    mwSize i;
    double *NNLtoutt;
...
       createNNLtriangle(NNLt, V, Fb, sn, nsn, nf, hs, bw, mw);    

       plhs[0] = mxCreateCellMatrix(nsn,50);
...
       for(i=0;i<nsn;i++){
//         copy(NNLt[i].begin(),NNLt[i].end(),NNLtout[i*50;i*50+NNLt[i].size()]);
//         NNLtoutt=mxCreatStrucMatrix(1,50,1,fnom);
           copy(NNLt[i].begin(),NNLt[i].end(),NNLtoutt);     
           mxSetCell(plhs[0],i,mxDuplicateArray(NNLtoutt));
       }

回答1:


mxDuplicateArray takes an mxArray pointer as input, not a double pointer.

If you want to copy your NNLt[i] vector into a MATLAB matrix and put that matrix into a cell array, you can do it like this:

for(...) {
  mxArray* tmp = mxCreatDoubleMatrix(1, NNLt[i].size(), mxREAL);
  copy(NNLt[i].begin(), NNLt[i].end(), mxGetPr(tmp));     
  mxSetCell(plhs[0], i, tmp);
}

You should not try to free the tmp matrix, let MATLAB take care of any memory that you allocated through the mx... functions.



来源:https://stackoverflow.com/questions/51622228/mexfunction-mxduplicatearray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!