MATLAB: Am I unable to compile the “timestwo” Fortran-MEX documentation example in MATLAB

buildfortranMATLABmex

I have saved the timestwo.F example in the documentation as timestwo.f on my system. This is the example located in $MATLAB\extern\examples\refbook, (where $MATLAB is the MATLAB root directory on your machine, as returned by typing
matlabroot at the MATLAB command prompt.
When I compile timestwo.f, I receive the following errors:
timestwo.f:22:
mwpointer plhs(*), prhs(*)
1 2
Unrecognized statement name at (1) and invalid form for assignment or statement-function definition at (2)
timestwo.f:23:
mwpointer mxGetPr, mxCreateDoubleMatrix
1 2
Unrecognized statement name at (1) and invalid form for assignment or statement-function definition at (2)
timestwo.f:24:
mwpointer x_pr, y_pr
1 2
Unrecognized statement name at (1) and invalid form for assignment or statement-function definition at (2)
timestwo.f:18:
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
1
timestwo.f:51: (continued):
plhs(1) = mxCreateDoubleMatrix(m,n,0)
2
Invalid declaration of or reference to symbol `plhs' at (2) [initially seen at (1)]
timestwo.f:52: warning:
x_pr = mxGetPr(prhs(1))
1
timestwo.f:53: (continued):
y_pr = mxGetPr(plhs(1))
2
Argument #1 of `mxgetpr' is one type at (2) but is some other type at (1) [info -f g77 M GLOBALS]
mex: compile of 'timestwo.f' failed.
??? Error using ==> mex
Unable to complete successfully

Best Answer

The reason for the compilation errors is that the original timestwo.F file contains preprocessor directives and #include statements that need to be processed by a preprocessor. When the Fortran file is renamed with a ".f" extension, the C preprocessor is not invoked and the preprocessor directives are not recognized, thus causing these errors. To compile the file successfully, first rename the file to timestwo.F.