mex return two values.

9 views (last 30 days)
HYUKJIN Song
HYUKJIN Song on 26 Jun 2014
Answered: Jan on 30 Jun 2014
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 Comments
James Tursa
James Tursa on 27 Jun 2014
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
Tejas M U on 30 Jun 2014
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

Sign in to comment.

Answers (1)

Jan
Jan on 30 Jun 2014
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).

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!