如何修复MEX文件中的非法字符错误

时间:2019-06-25 08:48:42

标签: matlab fortran mex

当我尝试在matlab上编译fortran代码时,我收到一些错误消息。

>> mex points.f
Warning: MATLAB FORTRAN MEX Files are now defaulting to -largeArrayDims and 8 byte integers.
     If you are building a FORTRAN S-Function, please recompile using the -compatibleArrayDims flag.
     You can find more about adapting code to use 64-bit array dimensions at:
     https://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html. 
Building with 'Intel Parallel Studio XE 2019 for Fortran with Microsoft Visual Studio 2017'.

Error using mex
C:\Users\Kinan\Desktop\Strathshare\Personal Folders\PhD\MATLABPERIDYNAMICS\points.f(44): error #5149: Illegal character in statement
label field  [r]
    re*8  dx, ral
----^

C:\Users\Kinan\Desktop\Strathshare\Personal Folders\PhD\MATLABPERIDYNAMICS\points.f(45): error #5149: Illegal character in statement
label field  [r]
    re*8 coordx, coordy, coordz
----^
>C:\Users\Kinan\Desktop\Strathshare\Personal Folders\PhD\MATLABPERIDYNAMICS\points.f(46): error #5149: Illegal character in statement
label field  [r]
    real*8 coord(totnode,3)
----^

实际代码是

#include "fintrf.h"
C======================================================================
C     points.f
C     Computational function that creates a cube of equdistant points
C     This is a MEX file for MATLAB.
C======================================================================


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

C     Declarations
      implicit none

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

C     Function declarations:
      mwPointer mxGetDoubles

      mwPointer mxCreateDoubleMatrix
      integer mxIsNumeric
      mwPointer mxGetM, mxGetN



C     Pointers to input/output mxArrays:
      mwPointer x_ptr, y_ptr




C     Array information:
      mwPointer mrows, ncols
      mwSize size






C     Arguments for computational routine:
    real*8  dx, r
    real*8 coordx, coordy, coordz 
    real*8 coord(totnode,3)
    real*8 ndivx, ndivy, ndivz
    integer i, j, k





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


 MX_HAS_INTERLEAVED_COMPLEX
      x_ptr = mxGetDoubles(prhs(1))

C     Create matrix for the return argument.
      plhs(1) = mxCreateDoubleMatrix(29791,3,0)
      y_ptr = mxGetDoubles(plhs(1))


      call points(coord,r,dx,ndivx,ndivy,ndivz)




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




      return
      end



C-----------------------------------------------------------------------
C     Computational routine

      subroutine points(coord,r,dx,ndivx,ndivy,ndivz)


C     Arguments for computational routine:
      real*8  dx, r, coordx, coordy, coordz 
    real*8 coord(totnode,3), ndivx, ndivy, ndivz
    integer i, j, k


do i = 1,ndivx
    do j = 1,ndivy
        do k = 1,ndivz
            coordx = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (i - 1) * dx
            coordy = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (j - 1) * dx
            coordz = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (k - 1) * dx
            nnum = nnum + 1
            coord(nnum,1) = coordx
            coord(nnum,2) = coordy
            coord(nnum,3) = coordz

        enddo        
    enddo
enddo

      return
      end

我需要执行一些for循环操作,因此,如果我可以获得工作模板,将会很有帮助。

对不起,我尝试添加更多错误消息,但提示我代码太多

1 个答案:

答案 0 :(得分:0)

我设法将代码MEX,但是有太多错误...

要禁止显示关于largeArrayDims的警告,可以执行:
warning('Off', 'MATLAB:mex:FortranLargeArrayDimsWarn_link');

注意:您的Fortran代码适用MX_HAS_INTERLEAVED_COMPLEX,因此您需要在-2018a命令中添加mex标志。 使用-2018a标志时,我找不到避免警告的方法。

MEX命令行使用-2018a标志:
mex -R2018a points.F

为了通过编译,我不得不对您的代码进行太多修改:
我在各行的开头添加了空格。
我删除了MX_HAS_INTERLEAVED_COMPLEX
我不知道如何处理totnode,所以我将其替换为值100
我不知道如何处理y_output,所以我将其替换为coord

这是经过修改的代码,可以通过编译:

#include "fintrf.h"
C======================================================================
C     points.f
C     Computational function that creates a cube of equdistant points
C     This is a MEX file for MATLAB.
C======================================================================


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

C     Declarations
      implicit none

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

C     Function declarations:
      mwPointer mxGetDoubles

      mwPointer mxCreateDoubleMatrix
      integer mxIsNumeric
      mwPointer mxGetM, mxGetN



C     Pointers to input/output mxArrays:
      mwPointer x_ptr, y_ptr

C     Array information:
      mwPointer mrows, ncols
      mwSize size



C     Arguments for computational routine:

      real*8  dx, r
      real*8 coordx, coordy, coordz 

C     What is totnode???
C     real*8 coord(totnode,3)
      real*8 coord(100,3)
      real*8 ndivx, ndivy, ndivz
      integer i, j, k





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


C MX_HAS_INTERLEAVED_COMPLEX
      x_ptr = mxGetDoubles(prhs(1))

C     Create matrix for the return argument.
      plhs(1) = mxCreateDoubleMatrix(29791,3,0)
      y_ptr = mxGetDoubles(plhs(1))


      call points(coord,r,dx,ndivx,ndivy,ndivz)




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





      return
      end



C-----------------------------------------------------------------------
C     Computational routine

      subroutine points(coord,r,dx,ndivx,ndivy,ndivz)


C     Arguments for computational routine:
      real*8 dx, r, coordx, coordy, coordz
C     What is totnode???      
C     real*8 coord(totnode,3), ndivx, ndivy, ndivz
      real*8 coord(100,3), ndivx, ndivy, ndivz
      integer i, j, k


      do i = 1,ndivx
        do j = 1,ndivy
          do k = 1,ndivz
            coordx = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (i - 1) * dx
            coordy = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (j - 1) * dx
            coordz = -1.0d0 / 2.0d0 * r + (dx / 2.0d0) + (k - 1) * dx
            nnum = nnum + 1
            coord(nnum,1) = coordx
            coord(nnum,2) = coordy
            coord(nnum,3) = coordz

          enddo
        enddo
      enddo

      return
      end

我希望它能帮助您继续发展。