MEX文件:如何返回已分配的Matlab数组 [英] Mex files: how to return an already allocated matlab array

查看:103
本文介绍了MEX文件:如何返回已分配的Matlab数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个非常棘手的问题,似乎很难解决.简而言之,我想从mex文件返回一个数组,该数组已作为mex函数输入传递.您可以这样做:

I have found a really tricky problem, which I can not seem to fix easily. In short, I would like to return from a mex file an array, which has been passed as mex function input. You could trivially do this:

void mexFunction(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[])
{
   pargout[0] = pargin[0];
}

但这不是我所需要的.我想从pargin[0]获取原始指针,在内部对其进行处理,并通过设置相应的数据指针来返回新创建的mex数组.像这样:

But this is not what I need. I would like to get the raw pointer from pargin[0], process it internally, and return a freshly created mex array by setting the corresponding data pointer. Like that:

#include <mex.h>

void mexFunction(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[])
{
  mxArray *outp;
  double *data;
  int m, n;

  /* get input array */
  data = mxGetData(pargin[0]);
  m = mxGetM(pargin[0]);
  n = mxGetN(pargin[0]);

  /* copy pointer to output array */
  outp = mxCreateNumericMatrix(0,0,mxDOUBLE_CLASS,mxREAL);
  mxSetM(outp, m);
  mxSetN(outp, n);
  mxSetData(outp, data);
  /* segfaults with or without the below line */
  mexMakeMemoryPersistent(data);
  pargout[0] = outp;
}

它不起作用.如果没有立即,我会收到段错误,然后打了几下电话.我相信文档 上对此一无所知.唯一的要求是data指针已经使用mxCalloc分配了,很明显它已经分配了.因此,我认为这段代码是合法的.

It doesn't work. I get a segfault, if not immediately, then after a few calls. I believe nothing is said about such scenario in the documentation. The only requirement is hat the data pointer has been allocated using mxCalloc, which it obviously has. Hence, I would assume this code is legal.

我需要这样做,因为我正在将复杂的MATLAB结构解析为内部C数据结构.我处理数据,有些数据被重新分配,有些则没有.我想透明地返回输出结构,而不用考虑何时只需要复制mxArray(第一个代码段)以及何时真正创建它.

I need to do this, because I am parsing a complicated MATLAB structure into my internal C data structures. I process the data, some of the data gets re-allocated, some doesn't. I would like to transparently return the output structure, without thinking when I have to simply copy an mxArray (first code snippet), and when I actually have to create it.

请帮助!

编辑

在与Amro进行进一步的查看和讨论之后,似乎甚至我的第一个代码段也不受支持,并且在某些情况下可能会导致MATLAB崩溃,例如,在将结构字段或单元元素传递给此类mex函数时:

After further looking and discussing with Amro, it seems that even my first code snippet is unsupported and can cause MATLAB crashes in certain situations, e.g., when passing structure fields or cell elements to such mex function:

>> a.field = [1 2 3];
>> b = pargin_to_pargout(a.field);   % ok - works and assigns [1 2 3] to b
>> pargin_to_pargout(a.field);       % bad - segfault

看来我将不得不走未公开说明的MATLAB"之路,并使用mxCreateSharedDataCopymxUnshareArray.

It seems I will have to go down the 'undocumented MATLAB' road and use mxCreateSharedDataCopy and mxUnshareArray.

推荐答案

您应使用 mxDuplicateArray ,那就是记录的方式:

You should use mxDuplicateArray, thats the documented way:

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    plhs[0] = mxDuplicateArray(prhs[0]);
}

这篇关于MEX文件:如何返回已分配的Matlab数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆