External Interfaces  A First Example -- Passing a Scalar

Let's look at a simple example of Fortran code and its MEX-file equivalent. Here is a Fortran computational routine that takes a scalar and doubles it.

• ```      subroutine timestwo(y, x)
real*8 x, y
C
y = 2.0 * x
return
end
```

Below is the same function written in the MEX-file format.

• ```C==============================================================
=
C     timestwo.f
C     Multiply the input argument by 2.
C
C     This is a MEX-file for MATLAB.
C     Copyright (c) 1984-2000 The MathWorks, Inc.
C     \$Revision: 1.12 \$
C==============================================================
=

C     Computational subroutine
subroutine timestwo(y, x)
real*8 x, y

y = 2.0 * x
return
end

C     The gateway routine
subroutine mexFunction(nlhs, plhs, nrhs, prhs)

integer mxGetM, mxGetN, mxGetPr
integer mxIsNumeric, mxCreateDoubleMatrix
integer plhs(*), prhs(*)
integer x_pr, y_pr
integer nlhs, nrhs
integer m, n, size
real*8 x, y

C     Check for proper number of arguments.
if(nrhs .ne. 1) then
call mexErrMsgTxt('One input required.')
elseif(nlhs .ne. 1) then
call mexErrMsgTxt('One output required.')
endif

C     Get the size of the input array.
m = mxGetM(prhs(1))
n = mxGetN(prhs(1))
size = m*n

C     Check to ensure the input is a number.
if(mxIsNumeric(prhs(1)) .eq. 0) then
call mexErrMsgTxt('Input must be a number.')
endif

C     Create matrix for the return argument.
plhs(1) = mxCreateDoubleMatrix(m, n, 0)
x_pr = mxGetPr(prhs(1))
y_pr = mxGetPr(plhs(1))
call mxCopyPtrToReal8(x_pr, x, size)

C     Call the computational subroutine.
call timestwo(y, x)

C     Load the data into y_pr, which is the output to MATLAB.
call mxCopyReal8ToPtr(y, y_pr, size)

return
end
```

To compile and link this example source file, at the MATLAB prompt type

• ```mex timestwo.f
```

This carries out the necessary steps to create the MEX-file called `timestwo` with an extension corresponding to the machine type on which you're running. You can now call `timestwo` as if it were an M-function.

• ```x = 2;
y = timestwo(x)
y =
4
``` Examples of Fortran MEX-Files Passing Strings © 1994-2005 The MathWorks, Inc.