emb*_*ert 4 matlab return-value mex
有没有办法从mex函数返回可变数量的输出?
有人可能将它们打包到一个单元格中,但我想知道是否有一种方法,以便它们直接在输出列表中扩展.就像是
a = mymex(arg1, arg2);
[a, b, c] = mymex(arg1, arg2, 'all');
Run Code Online (Sandbox Code Playgroud)
当然,您可以像任何其他MATLAB函数一样:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
for (int i=0; i<nlhs; i++) {
plhs[i] = mxCreateDoubleScalar(i);
}
}
Run Code Online (Sandbox Code Playgroud)
>> [a,b,c] = test_mex()
a =
0
b =
1
c =
2
Run Code Online (Sandbox Code Playgroud)
您将必须根据输入/输出确定要返回的参数数量,或者如果函数调用不正确则发出错误(输入不足,输出太多等等).上面的例子只接受任意数量的输出参数(包括无输出或零输出)
以逗号分隔的列表,允许用于以有趣的方式调用函数:
>> out = cell(1,20);
>> [out{:}] = test_mex()
out =
Columns 1 through 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Columns 12 through 20
[11] [12] [13] [14] [15] [16] [17] [18] [19]
Run Code Online (Sandbox Code Playgroud)
这就像调用20个输出变量的函数:
>> [x1,x2,...,x20] = test_mex()
Run Code Online (Sandbox Code Playgroud)
只是为了澄清,MEX函数就像常规的M函数一样,定义了可变数量的输入和输出(想想function varargout = mymex(varargin)),并且适用相同的规则; 由您来管理对输入的访问并创建必要的输出.
例如,前面的代码可以写成常规的M函数,调用方式与之前相同:
function varargout = test_fcn(varargin)
for i=1:nargout
varargout{i} = i-1;
end
end
Run Code Online (Sandbox Code Playgroud)
不同之处在于,如果您尝试访问超出范围的内容,或者尝试将输出写入超出实际分配的内容,则在MEX文件中可能会导致MATLAB崩溃.
举个例子:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
for (int i=0; i<30; i++) { // <--- note 30 as value
plhs[i] = mxCreateDoubleScalar(i);
}
}
Run Code Online (Sandbox Code Playgroud)
将上述内容称为:
>> test_mex % careful!
Run Code Online (Sandbox Code Playgroud)
很可能会导致MATLAB立即崩溃.虽然在M代码中完成同样的事情,但只会创建在调用后销毁的不必要的额外输出变量.
正如@chappjc在他的回答中解释的那样,对于MEX文件,你总是保证有至少一个输出的空间(至少plhs是一个mxArray*长度为1 的数组,因此plhs[0]无论如何都可以安全地分配).如果调用者指定了一个LHS变量,那么输出进入它,否则输出被分配给特殊ans变量(当然,如果输出为零,你仍然可以不分配).
幸运的是,没有分配足够的输出变量的相反情况被MATLAB捕获,并且引发了catchID 的常规错误MATLAB:unassignedOutputs(在MEX和M函数中).
此外,访问超出范围的输入将导致访问冲突(MATLAB将通过一个大的可怕对话框通知您,并且提示将转向"请重新启动MATLAB"消息).在常规M代码中执行相同操作,只会抛出一个常规错误"Index exceeds matrix dimensions.",没什么大不了的!
正如您所看到的,MEX世界中的事情很容易出错(以不可恢复的方式),因此您必须特别注意验证输入/输出参数.