如何将整数值作为 mex 函数的输入传递?
How to pass as input of a mex function an integer value?
我试图传递一个mexfunction的参数,一个整数,表示mxCreateDoubleMatrix的列数。这个整数不应该在主 mexFunction 之外的任何地方使用。
不知何故,这似乎不起作用。
// mex function for calling c++ code .
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char *input_buf;
size_t buflen;
double ncols;
double *result_final;
double *result_second;
/* get the length of the input string */
buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
/* copy the string data from prhs[0] into a C string input_ buf. */
input_buf = mxArrayToString(prhs[0]);
/* copy the int from prhs[0] to decide on length of the results. */
ncols = (int) (size_t) mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1, ncols, mxREAL);
plhs[1] = mxCreateDoubleMatrix(1, ncols, mxREAL);
result_final = mxGetPr(plhs[0]);
result_second = mxGetPr(plhs[1]);
/* Do the actual computations in a subroutine */
subroutine(input_buf, buflen, result_final, result_second);
}
如果我取出 ncols 线,其余所有内容都按预期工作。我没有将 ncols 作为子例程的输入,因为它实际上并没有在那里使用,而只是在 main 中用于定义输出数组的大小。
如果我调用 myfun('examplefile.txt',100),则输出数组的矩阵应该有一个 1x100 的矩阵,而不是调用结束时显示的矩阵具有无限/非常长的列数。
有人可以帮忙吗?
您正在将指向值的指针转换为size_t
,然后转换为int
。但它是一个指针,RAM中值位置的地址,而不是值本身。
ncols = (int) (size_t) mxGetPr(prhs[1]); %mex Get Pointer!!
改为获取值。
ncols = (int)(mxGetScalar(prhs[1]));
相关文章:
- 如果我更改函数输入,我的递归不起作用?
- 初始化与函数输入相关的全局数组
- 如何生成比用户使用 rand() 函数输入的数字大 5-10% 的数字?
- 将函数更改为具有设置函数输入变量的方法的类
- 如何避免将 const 和非 const 成员函数输入到模板中的代码重复
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- 迭代器作为C 中的函数输入
- 如何为作为雇员类的指针传入的构造函数输入数据
- 最小的函数输入和输出错误
- C++ 函数输入错误
- QComboBox 作为 QT 中的函数输入参数
- 函数输入之前的分割故障
- 如何在视觉工作室 2012年设置函数输入参数的颜色
- 函数输入变化
- 使用函数输入 1 个 int 和 1 个字符串数组并循环
- 将数组用于函数输入参数
- 如何在c++中将函数输入强制转换为智能指针
- 自定义检查器的覆盖率引用函数输入
- 为什么我不能使用两个scanf函数输入字母
- 犰狳cov函数输入