Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何从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个字节。



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 01:15


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.