主要内容

创建 Fortran 源 MEX 文件

此示例说明如何编写 MEX 文件以使用 MATLAB® 矩阵在 MATLAB 中调用 Fortran 子例程 timestwo。您可以在此处查看完整的源文件。此示例使用 MATLAB 编辑器编写源代码,并使用 MATLAB mex 命令创建 MEX 函数。

Fortran 子例程 timestwo

以下代码定义 timestwo 子例程,该子例程将 n 维数组 x_input 乘以 2,并以数组 y_output 形式返回结果。

      subroutine timestwo(y_output, x_input)
      real*8 x_input, y_output

      y_output = 2.0 * x_input
      return
      end

创建源文件

打开 MATLAB 编辑器,创建一个文件,并在 MEX 文件中记录以下信息。

C======================================================================
C     timestwo.f
C     Computational function that takes a scalar and doubles it.
C     This is a MEX file for MATLAB.
C======================================================================

添加包含 MATLAB API 函数声明的 Fortran 头文件 fintrf.h

#include "fintrf.h"

将文件保存在您的 MATLAB 路径(例如 c:\work)中,并将其命名为 timestwo.F。您的 MEX 文件名称为 timestwo

创建入口例程

MATLAB 使用入口例程 mexfunction 作为 Fortran 子例程的入口函数。添加以下 mexFunction 代码。

C     Gateway routine
      subroutine mexFunction(nlhs, plhs, nrhs, prhs)

C     Declarations

C     Statements

      return
      end

向您的 mexfunction 子例程添加以下语句以强制声明所有变量。

implicit none

显式类型声明对于 64 位数组是必需的。

声明 mexfunction 参量

要声明 mxArray 变量,请使用 MATLAB 类型 mwPointer。将以下代码添加到 Declarations 语句后。

C     mexFunction arguments:
      mwPointer plhs(*), prhs(*)
      integer nlhs, nrhs

声明函数和局部变量

  • 声明此 MEX 文件中使用的 MATLAB API 函数的符号名称和类型。

    C     Function declarations:
          mwPointer mxGetDoubles
          mwPointer mxCreateDoubleMatrix
          integer mxIsNumeric
          mwPointer mxGetM, mxGetN

    要确定函数的类型,请参考 MATLAB API 函数参考文档。有关示例,请参阅 mxGetDoubles 的文档。

  • 声明 mexfunction 参量的局部变量。

    C     Pointers to input/output mxArrays:
          mwPointer x_ptr, y_ptr
    
  • 声明矩阵变量。

    C     Array information:
          mwPointer mrows, ncols
          mwSize size
    

验证 MEX 文件的输入和输出参量

使用 nrhsnlhs 参量验证 MEX 文件输入和输出参量的数量。将以下语句添加到 mexfunction 代码块中。

C     Check for proper number of arguments. 
      if(nrhs .ne. 1) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:nInput',
     +                           'One input required.')
      elseif(nlhs .gt. 1) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:nOutput',
     +                           'Too many output arguments.')
      endif

使用 prhs 参量验证输入参量类型。

C     Check that the input is a number.
      if(mxIsNumeric(prhs(1)) .eq. 0) then
         call mexErrMsgIdAndTxt ('MATLAB:timestwo:NonNumeric',
     +                           'Input must be a number.')
      endif

创建计算例程

添加 timestwo 代码。以下子例程是您的计算例程,即用于执行您希望在 MATLAB 中使用的功能的源代码。

C     Computational routine

      subroutine timestwo(y_output, x_input)
      real*8 x_input, y_output

      y_output = 2.0 * x_input
      return
      end

计算例程是可选的。您也可以将代码置于 mexfunction 函数块中。

声明计算例程的变量

将以下变量声明置于 mexFunction 中。

C     Arguments for computational routine:
      real*8  x_input, y_output

读取输入数组

要指向输入矩阵数据,请使用 mxGetDoubles 函数。

x_ptr = mxGetDoubles(prhs(1))

要创建一个 Fortran 数组 x_input,请使用 mxCopyPtrToReal8 函数。

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

C     Create Fortran array from the input argument.
      call mxCopyPtrToReal8(x_ptr,x_input,size)

准备输出数据

要创建输出参量 plhs(1),请使用 mxCreateDoubleMatrix 函数。

C     Create matrix for the return argument.
      plhs(1) = mxCreateDoubleMatrix(mrows,ncols,0)

使用 mxGetDoubles 函数将 y_ptr 参量赋给 plhs(1)

      y_ptr = mxGetDoubles(plhs(1))

执行计算

将参量传递给 timestwo

C     Call the computational subroutine.
      call timestwo(y_output, x_input)

将结果复制到输出参量

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

查看完整的源文件

将源文件与位于 matlabroot/extern/examples/refbook 文件夹中的 timestwo.F 进行比较。在编辑器中打开文件

编译二进制 MEX 文件

在 MATLAB 命令提示符下,编译二进制 MEX 文件。

mex -R2018a timestwo.F

测试 MEX 文件

x = 99;
y = timestwo(x)
y =
   198

另请参阅

| | | | | | |

主题