Mex large output array
2 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
James Tursa
on 15 May 2013
Edited: James Tursa
on 17 May 2013
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 Comment
Jan
on 16 May 2013
Exactly. When the MEX function should return a matrix, let it return a matrix instead of a set of vectors.
More Answers (0)
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!