- Pass in a full real double 2D variable that is size MxN
- Inside the mex routine, pass this matrix along with its size to a subroutine
- Pass the results of this subroutine call back to MATLAB
How can I transfer the size of the array to mexFunction
1 view (last 30 days)
Show older comments
Igor Arkhandeev
on 8 May 2019
Edited: Igor Arkhandeev
on 11 May 2019
#include "fintrf.h"
SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs)
IMPLICIT NONE
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
mwPointer mxGetPr
mwPointer mxGetM
mwPointer mxCreateDoubleMatrix
mwPointer P1,P2,P3
REAL*8 R1,R2,R3
%R1 is integer number
%R2(R1,1) - size of R2
%R3(R1-2,11) - size of R3
P1 = mxGetPr(prhs(1))
P2 = mxGetPr(prhs(2))
CALL mxCopyPtrToREAL8(P1,R1,1)
CALL mxCopyPtrToREAL8(P2,R2,1)
plhs(1) = mxCreateDoubleMatrix(M-2,11,0)
P3 = mxGetPr(plhs(1))
CALL ProgramName(R1,R2,R3)
CALL mxCopyREAL8ToPtr(R3,P3,(M-2)*11)
return
end
This program has 2 input parameters: R1 and R2. Dimensions R2 = R2 (R1). Output parameter R3 has dimensions R3 (R1-2, 11). How can I implement this?
4 Comments
James Tursa
on 9 May 2019
Edited: James Tursa
on 9 May 2019
So, you still haven't answered my request to see the full signature of the programname subroutine, and identify which are inputs and which are outputs.
Accepted Answer
James Tursa
on 9 May 2019
Try this (caveat, untested). Don't pass in the row size of the matrix ... just pass in the matrix only. Let the code figure out the row size via mxGetM. I highly advise you add some argument checking into this as well. E.g., make sure you have exactly one full double 2D real input, and that nlhs <= 1.
#include "fintrf.h"
SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs)
IMPLICIT NONE
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
mwPointer mxGetPr
mwPointer mxGetM
mwPointer mxCreateDoubleMatrix
mwPointer P1, P3
integer M
mwSize Cm, Cn
integer*4 ComplexFlag
P1 = mxGetPr(prhs(1))
M = mxGetM(prhs(1))
if( M <= 2 ) then
call mexErrMsgTxt("Input size is too small")
endif
Cm = M - 2
Cn = 11
ComplexFlag = 0
plhs(1) = mxCreateDoubleMatrix(Cm,Cn,ComplexFlag)
P3 = mxGetPr(plhs(1))
CALL ProgramName( %val(P1), M, %val(P3) )
return
end
17 Comments
James Tursa
on 10 May 2019
So, I need to gently slap you on the wrist at this point. As you recall, I had put this in my original code for you:
if( M <= 2 ) then
call mexErrMsgTxt("Input size is too small")
endif
You chose not to implement this check, and it cost a lot of wasted time in tracking down the error. So ... I would strongly advise again to put in LOTS of argument checks in your mex routines in the future! :)
More Answers (1)
See Also
Categories
Find more on Matrices and Arrays 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!