Mex large output array

2 次查看(过去 30 天)
Chris
Chris 2013-5-15
Hi,
I want to have the output from my mex function stored in one large array, something like:
[output(:,:)] = mexfunction(input)
I have it working where I am receiving my output but I have to have the individual outputs listed
[y1,y2,y3...] = mexfunction(input)
I am using mxcopyreal8toptr to transfer the output back to matlab. Is there a different command I need to use to store in one large array?

采纳的回答

James Tursa
James Tursa 2013-5-15
编辑:James Tursa 2013-5-17
You have to combine the data into one array inside the mex routine itself. Once outside the mex routine, if all you have is the y1, y2, etc variables then you will have to manually concatenate them together (which will involve a data copy of course). I am guessing this is possible inside the mex routine with the mxCopyReal8ToPtr function (passing a pointer to the middle of a memory block) but confess I haven't tried it ... you may get an assert fault. If you do get a fault there are ways around it. Let me know if you need help with this.
EDIT
I just ran a test and mxCopyReal8ToPtr does not generate an assert fault if passed an address in the middle of a memory block. So a code snippet example for doing this is:
mwPointer mxCreateDoubleMatrix
mwPointer mxGetPr
:
mwPointer pr
real*8 x(3), y(3) ! start with two separate Fortran variables
mwSize m, n
integer*4 complexity
:
x = [1,2,3]
y = [4,5,6]
m = 3
n = 2
complexity = 0
plhs(1) = mxCreateDoubleMatrix(m,n,complexity)
pr = mxGetPr(plhs(1)) ! point to 1st column
call mxCopyReal8ToPtr(x,pr,m) ! copy into the 1st column
pr = pr + m*8 ! pointer arithmetic, point to 2nd column
call mxCopyReal8ToPtr(y,pr,m) ! copy into the 2nd column
  1 个评论
Jan
Jan 2013-5-16
Exactly. When the MEX function should return a matrix, let it return a matrix instead of a set of vectors.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by