Error with simple mex file
6 views (last 30 days)
Show older comments
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
Accepted Answer
James Tursa
on 17 May 2012
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
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.
More Answers (1)
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
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) 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!