Error with simple mex file

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

 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

Thanks alot James. Do I just need to removed the /fixed option from the .bat file to allow .F90 files to be mexed
Yes. That's all you need to do. I have no idea why the /fixed option is in the COMPFLAGS line in the first place.
I deleted the /fixed and changed the file extensions to .F90 but now I get the following:
>> mex doublevectorg.F90 doublevectorf.F90
ifort: command line warning #10161: unrecognized source type 'OPTIMFLAGS=/O2'; object file assumed
doublevectorg.F90(2): error #5082: Syntax error, found '==' when expecting one of: ( % [ : . = =>
C======================================================================
-^
doublevectorg.F90(9): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % [ : . = =>
C
------^
doublevectorg.F90(10): error #5082: Syntax error, found IDENTIFIER 'MATSQ' when expecting one of: ( % [ : . = =>
C matsq.f
------^
doublevectorg.F90(11): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % [ : . = =>
C
-^
doublevectorg.F90(12): error #5082: Syntax error, found IDENTIFIER 'SQUARES' when expecting one of: ( % [ : . = =>
C squares the input matrix
------^
doublevectorg.F90(14): error #5082: Syntax error, found IDENTIFIER 'THIS' when expecting one of: ( % [ : . = =>
C This is a MEX-file for MATLAB.
------^
doublevectorg.F90(15): error #5082: Syntax error, found IDENTIFIER 'COPYRIGHT' when expecting one of: ( % [ : . = =>
C Copyright 1984-2011 The MathWorks, Inc.
------^
doublevectorg.F90(15): error #5082: Syntax error, found IDENTIFIER 'THE' when expecting one of: * ) :: , <END-OF-STATEMENT> ; . (/ + - : ] /) ' ** / // > .LT. < ...
C Copyright 1984-2011 The MathWorks, Inc.
--------------------------^
doublevectorg.F90(15): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: <IDENTIFIER> [
C Copyright 1984-2011 The MathWorks, Inc.
---------------------------------------------^
doublevectorg.F90(16): error #5082: Syntax error, found IDENTIFIER '$REVISION' when expecting one of: ( % [ : . = =>
C $Revision: 1.13.2.5 $
------^
doublevectorg.F90(17): error #5082: Syntax error, found '==' when expecting one of: ( % [ : . = =>
C======================================================================
-^
doublevectorg.F90(18): error #5082: Syntax error, found IDENTIFIER 'GATEWAY' when expecting one of: ( % [ : . = =>
C Gateway routine
------^
doublevectorg.F90(21): error #5082: Syntax error, found IDENTIFIER 'DECLARATIONS' when expecting one of: ( % [ : . = =>
C Declarations
------^
doublevectorg.F90(24): error #5082: Syntax error, found IDENTIFIER 'MEXFUNCTION' when expecting one of: ( % [ : . = =>
C mexFunction arguments:
------^
doublevectorg.F90(28): error #5082: Syntax error, found IDENTIFIER 'FUNCTION' when expecting one of: ( % [ : . = =>
C Function declarations:
------^
doublevectorg.F90(33): error #5082: Syntax error, found IDENTIFIER 'POINTERS' when expecting one of: ( % [ : . = =>
C Pointers to input/output mxArrays:
------^
doublevectorg.F90(36): error #5082: Syntax error, found IDENTIFIER 'ARRAY' when expecting one of: ( % [ : . = =>
C Array information:
------^
doublevectorg.F90(40): error #5082: Syntax error, found IDENTIFIER 'ARGUMENTS' when expecting one of: ( % [ : . = =>
C Arguments for computational routine:
------^
doublevectorg.F90(41): error #5082: Syntax error, found IDENTIFIER 'MAXIMUM' when expecting one of: ( % [ : . = =>
C Maximum size = numel
------^
doublevectorg.F90(46): error #5082: Syntax error, found '-' when expecting one of: ( % [ : . = =>
C-----------------------------------------------------------------------
-^
doublevectorg.F90(46): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> <INTEGER_CONSTANT> ...
C-----------------------------------------------------------------------
------------------------------------------------------------------------^
doublevectorg.F90(47): error #5082: Syntax error, found IDENTIFIER 'CHECK' when expecting one of: ( % [ : . = =>
C Check for proper number of arguments.
------^
doublevectorg.F90(49): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( * ) :: , <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> ...
call mexErrMsgIdAndTxt ('MATLAB:matsq:nInput',
-------------------------------------------------------^
doublevectorg.F90(50): error #5276: Unbalanced parentheses
+ 'One input required.')
------------------------------------------------------^
doublevectorg.F90(52): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( * ) :: , <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> ...
call mexErrMsgIdAndTxt ('MATLAB:matsq:nOutput',
--------------------------------------------------------^
doublevectorg.F90(53): error #5276: Unbalanced parentheses
+ 'One output required.')
-------------------------------------------------------^
doublevectorg.F90(56): error #5082: Syntax error, found IDENTIFIER 'GET' when expecting one of: ( % [ : . = =>
C Get the size of the input array.
------^
doublevectorg.F90(61): error #5082: Syntax error, found IDENTIFIER 'COLUMN' when expecting one of: ( % [ : . = =>
C Column * row must be smaller than numel
------^
doublevectorg.F90(63): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( * ) :: , <IDENTIFIER> <CHAR_CON_KIND_PARAM> <CHAR_NAM_KIND_PARAM> <CHARACTER_CONSTANT> ...
call mexErrMsgIdAndTxt ('MATLAB:matsq:mSize',
------------------------------------------------------^
doublevectorg.F90(64): error #5276: Unbalanced parentheses
+ 'Row * column must be <= 1000.')
----------------------------------------------------------------^
C:\Users\Student\AppData\Local\Temp\33642.i90(275): catastrophic error: Too many errors, exiting
compilation aborted for doublevectorg.F90 (code 1)
C:\PROGRA~1\MATLAB\R2011B\BIN\MEX.PL: Error: Compile of 'doublevectorg.F90' failed.
Error using mex (line 206)
Unable to complete successfully.
Any idea?
OK, I was not trying to imply that you can take a fixed format file and turn it into a free format file just by changing the extension. That will not work in general, but that is what you did. The source code you have *is* a fixed format file (has comments with a C in column 1, etc) so it needs to have a .F extension. What I was trying to tell you was that if you *did* have a free format file with a .F90 extension that was written correctly for free format, it would not mex correctly unless you had fist removed the /fixed options from the mexopts.bat file. For your specific case, just do the code corrections I suggested and leave it with a .F extension
Hi James, Your msg understood. I now have the following problem trying to use code written in free form. I get the following error:
ifort: command line warning #10161: unrecognized source type 'OPTIMFLAGS=/O2'; object file assumed
ifort: warning #10146: no action performed for specified object file(s)
ifort: command line warning #10161: unrecognized source type 'OPTIMFLAGS=/O2'; object file assumed
doublevector.f90(3): error #7002: Error in opening the compiled module file. Check INCLUDE paths. [VECTORTWO]
use vectortwo
----^
any idea for error code 10161? Also can you use mex with a subroutine using a fortran module?
What does your mex command line look like? It appears that you have put the OPTIMFLAGS=/O2 stuff on the command line incorrectly and the compiler thinks it is a file name.
Delete the OPTIMFLAGS=/O2 part from the make file or mex options file (as appropriate)
I have no idea at the moment about the "use" statement.
@Walter: The "use vectortwo" is a valid Fortran statement for using a pre-compiled module named vectortwo. It is part of the source code. Presumably the code that would have been compiled to produce this module didn't compile, so when the compiler saw this "use vectortwo" statement it generated an error since the vectortwo module object file was not found.
It works when I deleted the OPTIMFLAGS=/O2 line as Walter suggested. I fixed the gatewayv.f90 code and now I can mex and created the gatewayv.mexw64 file. However when I'm trying to use it, matlab is taking along to execute the command. Is this normal for trying to mex with fortran modules?
*a long time not *along to execute the command
Can you post your current code?
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.
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.
James,
Got it. It works now and I compiled the module first. Thanks alot for your help and tips.

Sign in to comment.

More Answers (1)

hojjat
hojjat on 26 May 2013

0 votes

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. ************************************************************************
>>

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!