Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Error with simple mex file

Asked by Chris on 17 May 2012

Hello,

I'm learning about mex files and trying to create a mex file that would double an input array from Fortran. I want the mex file to call the computational subroutine like in the matlab examples but I keep getting errors when the subroutine is compiled. The gateway is compiled just fine but the errors occur on the subroutine and I can't see why. Please see below errors:

>> mex doublevg.F doublevf.F

doublevf.F(3): error #5149: Illegal character in statement label field [s] subroutine doublev(y, x, m, n) -^ doublevf.F(3): error #5149: Illegal character in statement label field [u] subroutine doublev(y, x, m, n) --^ doublevf.F(3): error #5149: Illegal character in statement label field [b] subroutine doublev(y, x, m, n) ---^ doublevf.F(3): error #5149: Illegal character in statement label field [r] subroutine doublev(y, x, m, n) ----^ doublevf.F(3): error #5118: First statement in file must not be continued subroutine doublev(y, x, m, n) -----^ doublevf.F(3): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % [ . = => subroutine doublev(y, x, m, n) -------------------------------^

doublevf.F(10): error #6353: A RETURN statement is invalid in the main program. return ------^

doublevf.F(7): error #6321: An unterminated block exists. do j=1,n

doublevf.F(6): error #6321: An unterminated block exists. do i=1,m

doublevf.F(4): error #6591: An automatic object is invalid in a main program. [Y] real*8 x(m,n), y(m,n)

doublevf.F(4): error #6591: An automatic object is invalid in a main program. [X] real*8 x(m,n), y(m,n)

doublevf.F(4): error #6219: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association [M] real*8 x(m,n), y(m,n)

doublevf.F(4): error #6219: A specification expression object must be a dummy argument, a COMMON block object, or an object accessible through host or use association [N] real*8 x(m,n), y(m,n)

compilation aborted for doublevf.F (code 1)

C:\PROGRA~1\MATLAB\R2011B\BIN\MEX.PL: Error: Compile of 'doublevf.F' failed. 

Error using mex (line 206) Unable to complete successfully.

Here is the fortran subroutine. It's from the matsq.F example but just changed so the input array is doubled instead of squared.

!doublev computational routine

 subroutine doublev(y, x, m, n)
      real*8 :: x(m,n), y(m,n)
      do  i=1,m
         do j=1,n
            y(i,j)= x(i,j)*2
      return
      end

Any help would be appreciated.

Chris

0 Comments

Chris

Products

No products are associated with this question.

2 Answers

Answer by James Tursa on 17 May 2012
Accepted answer

For a file ending with extension .F the compiler assumes fixed form format for the source code. This means:

Columns 1-5 are the label field

Column 6 is the continuation field

Columns 7-72 are the code field

In the following line of your source code, the word "subroutine" is in the first 5 columns, hence the error for illegal characters in the label field:

subroutine doublev(y, x, m, n)

Move this line over so that the text starts in column 7 (or higher):

        subroutine doublev(y, x, m, n)

If you want to use a free form format with Fortran files (allows code to be in any columns among other things), use the extension .F90 instead of .F (but you will have to remove the /fixed option from the mexopts.bat file in order for this to work).

Also, your do loops are not terminated. E.g.,

        do  i=1,m
           do j=1,n
              y(i,j)= x(i,j)*2

should be

        do  i=1,m
           do j=1,n
              y(i,j)= x(i,j)*2
          enddo
        enddo

Or the entire double loop can be replaced with:

        y = 2 * x

14 Comments

Chris on 22 May 2012

Gateway Routine:
#include <fintrf.h>

!gateway routine
subroutine mexFunction (nlhs, plhs, nrhs, prhs)

implicit none

mwpointer plhs(*), prhs(*)

integer*8 nlhs, nrhs

mwpointer mxcreatedoublematrix, mxgetpr
mwpointer mxgetm, mxgetn

mwpointer x_pr, y_pr

!array information
mwpointer m, n
mwsize size

!computational arguments
real*8, dimension(3) :: x_input, y_output

!get size on input array
m = mxgetm(prhs(1))
n = mxgetn(prhs(1))
size = m*n

!Create fortran input array
x_pr = mxgetpr(prhs(1))
call mxcopyptrtoreal8(x_pr,x_input,size)

!Create matrix for return argument
plhs(1) = mxcreatedoublematrix(m,n,0)
y_pr = mxgetpr(plhs(1))

!subroutine
call doublevector(x_input)

!copy to output array
call mxcopyreal8toptr(y_output,y_pr,size)

return
end

subroutine
subroutine doublevector(x)

use vectortwo

real*8, dimension(3) :: x_input, y_output

y_output = doublev(x_input)

return
end

module:
module vectortwo
!Module contains vector times two function

implicit none

Contains

function doublev(x)

real*8, intent(in), dimension(3) :: x
real*8, dimension(3) :: doublev

doublev = x * 2

end function doublev

end module vectortwo
*********************************
The problem now is the mex function returns 0's for the answer; such as I do gatewayv(x) with x = 4,3,2, - it returns 0,0,0.

James Tursa on 23 May 2012

1) The module needs to be compiled before it is used. So the code for module vectortwo needs to be compiled before the compiler sees the "use vectortwo" line. So either put the module code at the top of your source file, or put it in a separate file that gets compiled first.

2) You don't put anything in y_output, so garbage (0's in your case) gets copied into y_pr. You need to do something like this:

call doublevector(y_output,x_input)
:
subroutine doublevector(y_output,x_input)

3) Get in the habit of putting "implicit none" in every single subroutine, function, module, and main program.

4) Just a suggestion, but comments are much more readable if they are offset from the code somewhat. Your brain doesn't have to work as hard to separate the comments from the code. My style is as follows:

!\
! The comment text.
!/

Your code will be a lot easier to read if the comments are not packed tight in with the code.

Chris on 23 May 2012

James,

Got it. It works now and I compiled the module first. Thanks alot for your help and tips.

James Tursa
Answer by hojjat on 26 May 2013

hello

this is error in building mex-file in my pc please help me!

mex -setup

Welcome to mex -setup. This utility will help you set up a default compiler. For a list of supported compilers, see http://www.mathworks.com/support/compilers/R2013a/win32.html

Please choose your compiler for building MEX-files:

Would you like mex to locate installed compilers [y]/n? y

Select a compiler: [1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2013a\sys\lcc [2] Microsoft Software Development Kit (SDK) 7.1 in C:\Program Files\Microsoft Visual Studio 10.0 [3] Microsoft Visual C++ 2010 in C:\Program Files\Microsoft Visual Studio 10.0

[0] None

Compiler: 3

Please verify your choices:

Compiler: Microsoft Visual C++ 2010 Location: C:\Program Files\Microsoft Visual Studio 10.0

Are these correct [y]/n? y

************************************************************************* Warning: MEX-files generated using Microsoft Visual C++ 2010 require that Microsoft Visual Studio 2010 run-time libraries be available on the computer they are run on. If you plan to redistribute your MEX-files to other MATLAB users, be sure that they have the run-time libraries. *************************************************************************

Trying to update options file: C:\Users\eng\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat From template: C:\PROGRA~1\MATLAB\R2013a\bin\win32\mexopts\msvc100opts.bat

Done . . .

************************************************************************ Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html Building with the -largeArrayDims option enables the new API. ************************************************************************

>>

0 Comments

hojjat

Contact us