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

查看:31
本文介绍了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天全站免登陆