mex return two values.
4 次查看(过去 30 天)
显示 更早的评论
hi.
i am trying to mex(in fortran-matlab).
i want to two values in matlab using mex, i make fortran and put in to 4 mwpointer x-input, y-output, a-input, b-input.
and make fortran files, but it can't read two values.
========================================================================================================
real*8 x, y, A, B, z
C-----------------------------------------------------------------------
if(nrhs .ne. 2) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:nInput',
+ 'One input required.')
elseif(nlhs .gt. 2) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:nOutput',
+ 'Too many output arguments.')
endif
if(mxIsNumeric(prhs(2)) .eq. 0) then
call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric',
+ 'Input must be a number.')
endif
mrows = mxGetM(prhs(1))
ncols = mxGetN(prhs(2))
size = mrows*ncols
x_ptr = mxGetPr(prhs(1))
A_ptr = mxGetPr(prhs(2))
call mxCopyPtrToReal8(x_ptr,x,size)
call mxCopyPtrToReal8(A_ptr,A,size)
plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)
y_ptr = mxGetPr(plhs(1))
B_ptr = mxGetPr(plhs(2))
call timestwo(y, x, B, A, z)
call mxCopyReal8ToPtr(y,y_ptr,size)
call mxCopyReal8ToPtr(B,B_ptr,size)
return
end
C----------------------------------------------------------------------- C Computational routine
subroutine timestwo(y, x, B, A, z)
real*8 y, x, B, A, z
y = 2.0 * x
B = 2.0 * A
i = y, j = b
z=(i,j)
return
end
======================================================================================================
i'm so hard to understand about prhs,plhs..
is this program correct?
help me please.
3 个评论
James Tursa
2014-6-27
Rather than comment on the multiple problems with the above routine, may I start with asking you what you want it to do? Are you simply trying to input two double scalars, multiply both of them by 2, and then return both of the results back to the MATLAB workspace?
Tejas M U
2014-6-30
If you want to understand about prhs, plhs, you may want to refer to the following: http://www.mathworks.com/help/matlab/matlab_external/data-flow-in-fortran-mex-files.html
回答(1 个)
Jan
2014-6-30
You have created the 1st output by:
plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)
An equivalent method is required for the 2nd output also, only with plhs(2).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!