matlab mex 矩阵,如何从mex函数访问matlab结构字段中的矩阵?

icon1.gif 如何从mex函数访问matlab结构字段中的矩阵?

我试图弄清楚如何从mex函数访问存储在matlab结构中的字段中的矩阵。

那真是漫长的缠绕...让我解释一下:

我有一个定义如下的matlab结构:

matrixStruct = struct('matrix', {4, 4, 4; 5, 5, 5; 6, 6 ,6}) 我有一个mex函数,在其中我希望能够收到指向矩阵中第一个元素的指针(用c表示的矩阵[0] [0]),但是我一直无法弄清楚该怎么做那。

我尝试了以下方法:

/* Pointer to the first element in the matrix (supposedly)... */ double *ptr = mxGetPr(mxGetField(prhs[0], 0, "matrix"); /* Incrementing the pointer to access all values in the matrix */ for(i = 0; i < 3; i++){ printf("%f\n", *(ptr + (i * 3))); printf("%f\n", *(ptr + 1 + (i * 3))); printf("%f\n", *(ptr + 2 + (i * 3))); } 最终打印出以下内容:

4.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 我还尝试了以下方法的变体,以为嵌套函数调用可能有点奇怪,但无济于事:

/* Pointer to the first location of the mxArray */ mxArray *fieldValuePtr = mxGetField(prhs[0], 0, "matrix"); /* Get the double pointer to the first location in the matrix */ double *ptr = mxGetPr(fieldValuePtr); /* Same for loop code here as written above */ 有人对我如何实现自己正在尝试的目标,或者我可能做错的事情有想法吗?

谢谢!

编辑:根据yuk的评论,我尝试对具有名为array的字段的结构进行类似的操作,该字段是一维双精度数组。

包含数组的结构定义如下:

arrayStruct = struct('array', {4.44, 5.55, 6.66}) 我在mex函数中对arrayStruct进行了以下尝试:

mptr = mxGetPr(mxGetField(prhs[0], 0, "array")); printf("%f\n", *(mptr)); printf("%f\n", *(mptr + 1)); printf("%f\n", *(mptr + 2)); ...但是输出跟在先前打印的内容之后:

4.440000 0.000000 0.000000

回答:

您正在尝试访问MATLAB中的单元格数组变量。您确定数据会因此存储吗?如果将双数组放入结构中会发生什么?

matrixStruct = struct('matrix', [4, 4, 4; 5, 5, 5; 6, 6 ,6]) 我认为问题在于MATLAB如何在单元格数组中存储数据。尝试运行此命令:

double1 = 1; double2 = 1:2; cellempty = {[]}; celldouble1 = {1}; celldouble2 = {1:2}; cell2doubles = {1,2}; whos 输出:

Name Size Bytes Class Attributes cell2doubles 1x2 136 cell celldouble1 1x1 68 cell celldouble2 1x1 76 cell cellempty 1x1 60 cell double1 1x1 8 double double2 1x2 16 double 您可以看到单元格数组的每个元素占用数字大小额外的60个字节。

更多&回答...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值