Hello, I'm having trouble getting a mex function to work. I think I've narrowed down the problem but anytime I try to fix anything Matlab shuts down.
I want to send a nested struct through a mex file myvar.myfield.mysubfield, where subfield is a 3-d position vector. I want to do mymexfunc(myvar).
mwPointer mxGetField
mwPointer mxGetPr
mwPointer mxcreatedoublematrix
mwIndex index
mwpointer var_ptr, fd_ptr, sfd_ptr
var_ptr = mxgetpr(prhs(1))
fd_ptr = mxgetfield(var_ptr,1,'myfield')
sfd_ptr = mxgetfield(fd_ptr,1,'mysubfield')
myvar%myfield%mysubfield = fpgetpr(sfd_ptr)
copy mxcopyptrtoreal8(sfd_ptr,myvar%myfield%mysubfield,size)
Matlab crashes when I try to do this and I wanted to know if anyone can see what I'm doing wrong.

 採用された回答

James Tursa
James Tursa 2012 年 7 月 9 日
編集済み: James Tursa 2012 年 7 月 9 日

0 投票

mwPointer mxGetField
mwPointer mxGetPr
mwPointer mxcreatedoublematrix
mwIndex index
mwpointer var_ptr, fd_ptr, sfd_ptr
fd_ptr = mxgetfield(prhs(1),1,'myfield') ! Result is mxArray pointer
sfd_ptr = mxgetfield(fd_ptr,1,'mysubfield') ! Result is mxArray pointer
var_ptr = mxgetpr(sfd_ptr) ! Result is real*8 pointer
copy mxcopyptrtoreal8(var_ptr,myvar%myfield%mysubfield,size)
I would use different names for the "size" and "index" variables, since those are names of Fortran intrinsic functions.

7 件のコメント

Chris
Chris 2012 年 7 月 10 日
Hi James,
For copying data back to matlab, would it be as follows if I received the following data: myvar.myfield1,subfield1, myvar.myfield1.subfield2, myvar.myfield2.subfield1?
plhs(1) = mxcreatedoublematrix(m1,n1,0)
plhs(2) = mxcreatedoublematrix(m2,n2,0)
plsh(3) = mxcreatedoublematrix(m3,n3,0)
output1_ptr = mxgetpr(plhs(1))
output2_ptr = mxgetpr(plhs(2))
output3_ptr = mxgetpr(plhs(3))
call mxcopyreal8toptr(output1_ptr, myvar.mfield1.subfield1, size1)
call mxcopyreal8toptr(output2_ptr, myvar.mfield1.subfield2, size2)
call mxcopyreal8toptr(output3_ptr, myvar.mfield2.subfield1, size3)
James Tursa
James Tursa 2012 年 7 月 10 日
Unclear what you want. Do you want to return a single variable with myfield1, myfield2, and subfield1? Or do you want to return three variables?
Chris
Chris 2012 年 7 月 10 日
I want to return the three variables to varify that the data was passed correctly.
Chris
Chris 2012 年 7 月 10 日
It also still crashes with the "segmentation violation" error
James Tursa
James Tursa 2012 年 7 月 10 日
You have the arguments mixed up. Try this:
call mxcopyreal8toptr(myvar%mfield1%subfield1, output1_ptr, size1)
call mxcopyreal8toptr(myvar%mfield1%subfield2, output2_ptr, size2)
call mxcopyreal8toptr(myvar%mfield2%subfield1, output3_ptr, size3)
Chris
Chris 2012 年 7 月 10 日
Thanks James, I didn't see that. The single nested struct now works but not the mex calling for multiple values like above. I think it has to do with the size input. Should it be as follows?
!this works for the single nested struct mex
m = mxgetm(sfd_ptr)
n = mxgetn(sfd_ptr)
size1 = m*n
!but it seems like I should use the real*8 pointer
m = mxgetm(var_ptr)
n = mxgetn(var_ptr)
size1 = m*n
They both don't work for the multi nested struct values and make matlab crash.
Chris
Chris 2012 年 7 月 10 日
I figured it out James, it was crashing because I had the wrong index value for the mxGetField. I thought I needed index=2 for myfield2 but I used 1 for them all and it works. Thanks for all your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by