Come come. Mex File error running.

2 views (last 30 days)
Zhong Hao
Zhong Hao on 6 Jun 2014
Edited: James Tursa on 6 Jun 2014
Hi, I've been stuck on this mexfunction(fortran) for quite a while.
I can compile the fortran source into a mex file but I can't run it as a mex file. I've tried the 95InterfaceToMatlabAPI, %val method and the mxCopy... method, but to no avail.
Can you spot my mistake?
#include "fintrf.h"
**********************************************************************
#if 0
* mextest.F *
* .F file needs to be preprocessed to generate .for equivalent *
* *
#endif
* *
**********************************************************************
* GATEWAY ROUTINE
SUBROUTINE mexFunction(nlhs, plhs, nrhs, prhs)
* DECLARATIONS:
IMPLICIT NONE
* MEXFUNCTION ARGUMENTS:
MWPOINTER plhs(*), prhs(*)
INTEGER nlhs, nrhs
* DECLARE SUBFUNCTIONS USED IN MEXFUNCTION
MWPOINTER mxGetPr
MWPOINTER mxCreateDoubleMatrix
* DECLARE LOCAL VARIABLES USED IN MEXFUNCTION:
MWPOINTER in1, in2, in3, in4, in5, in6, in7, in8, in9, in10,
& in11, in12, in13, in14,
& out1, out2
MWSIZE one
INTEGER*4 nil
one = 1
nil = 0
* PREPARE INPUT MATRIX .
in1=mxGetPr(prhs(1))
in2=mxGetPr(prhs(2))
in3=mxGetPr(prhs(3))
in4=mxGetPr(prhs(4))
in5=mxGetPr(prhs(5))
in6=mxGetPr(prhs(6))
in7=mxGetPr(prhs(7))
in8=mxGetPr(prhs(8))
in9=mxGetPr(prhs(9))
in10=mxGetPr(prhs(10))
in11=mxGetPr(prhs(11))
in12=mxGetPr(prhs(12))
in13=mxGetPr(prhs(13))
in14=mxGetPr(prhs(14))
* PREPARE OUTPUT MATRIX
plhs(1) = mxCreateDoubleMatrix(one,one,nil)
out1 = mxGetPr(plhs(1))
plhs(2) = mxCreateDoubleMatrix(one,one,nil)
out2 = mxGetPr(plhs(2))
* CALL THE COMPUTATIONAL ROUTINE
call test(%val(in1),%val(in2),%val(in3),
& %val(in4),%val(in5),%val(in6),%val(in7),
& %val(in8),%val(in9),%val(in10),%val(in11),
& %val(in12),%val(in13),%val(in14),
& %val(out1),%val(out2))
RETURN
END
*************************************************************************
* COMPUTATIONAL SUBROUTINE
SUBROUTINE test(in1, in2, in3, in4, in5, in6, in7, in8, in9,
& in10, in11, in12, in13, in14, out1, out2)
REAL in1, in2, in3, in4, in5, in7, in8, in10, in11, in13,
& in14, out1, out2
INTEGER in6, in9, in12
out1 = in1
RETURN
END
*************************************************************************

Answers (1)

James Tursa
James Tursa on 6 Jun 2014
Edited: James Tursa on 6 Jun 2014
You've got several type mismatches. E.g.,
out2 in mexFunction is a pointer to a double (i.e., REAL*8) since you use mxCreateDoubleMatrix to create the underlying mxArray variable. You pass this address to the computational subroutine with %val(out2), but then in the subroutine you declare that same variable as single (i.e., REAL which is the same as REAL*4). So this will not work because of the mismatch.
Same comment for out1. It is pointing to a REAL*8 via the mxArray creation, but in the subroutine it is declared as a REAL (which is the same as REAL*4).
In fact I would question ALL of your inputs as well. Unless you are explicitly declaring the inputs at the MATLAB calling level as single (for the REAL inputs) and int32 (for the INTEGER inputs) for in1, in2, etc., things will not match up and will not work.
Double variables in MATLAB (e.g. all numeric literal constants in MATLAB are double) need to be matched with REAL*8 inside the mex function. Single variables need to be matched with REAL*4. int32 variables need to be matched with INTEGER (or INTEGER*4). Etc.
So you need to scrub your code to make sure all of the variables match up in type and are exactly what is passed in from the MATLAB calling routine.

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!