Hello Everyone!
I'm trying to access MatLab-Functions from an MatLab generated .dll-file.
I discovered an example program, which now works with Digital Fortran, but not with ftn95, which I have to use now.
Since I'm not a very experienced fortran-programmer, I need some help.
If you can help me in any way or point me to documentation, I would be very pleased. [size=9:645ad5aa41]
program test
use kernel32
IMPLICIT NONE
INTERFACE
SUBROUTINE matlabinc(nlhs,plhs, nrhs,prhs)
integer*4 :: nlhs,nrhs
integer*4 :: plhs,prhs
!DEC$ ATTRIBUTES C, DLLIMPORT :: matlabinc
!DEC$ ATTRIBUTES VALUE :: nlhs,nrhs
!DEC$ ATTRIBUTES REFERENCE :: plhs,prhs
END SUBROUTINE
SUBROUTINE matlibInitialize()
!DEC$ ATTRIBUTES C, DLLIMPORT :: matlibInitialize
END SUBROUTINE
SUBROUTINE matlibTerminate()
!DEC$ ATTRIBUTES C, DLLIMPORT :: matlibTerminate
END SUBROUTINE
END INTERFACE
! This part only required for our own Matlab routine, not the mx* routines
integer :: p_matlib_dummy, p_libmx_dummy
logical :: status
! matlib.dll
pointer (p_matlib,p_matlib_dummy)
pointer (pr1_matlib,matlibInitialize)
pointer (pr2_matlib,matlabinc)
pointer (pr3_matlib,matlibTerminate)
! Declare all mx* routines we call
INTEGER*4 :: mxGetData !
INTEGER*4 :: mxGetNumberOfElements !
INTEGER*4 :: mxCreateScalarDouble !
INTEGER*4 :: mxCreateNumericMatrix !
INTEGER*4 :: mxClassIDFromClassName !
real*8 :: a(4) ! What I store in Matlab mx array
real*8 :: b(4) ! What I read out of Matlab mx array
integer*4 :: a_mx_p ! Pointer to mx array for a
integer*4 :: a_dx_p ! Pointer to data in mx array for a
integer*4 :: b_mx_p ! Pointer to mx array for b
integer*4 :: b_dx_p ! Pointer to data in mx array for b
integer*4 :: na ! Number of entries in a
integer*4 :: nb ! Number of entries in b
p_matlib = loadlibrary('matlib.dll'C)
pr1_matlib = getprocaddress(p_matlib, '_matlibInitialize'C)
pr2_matlib = getprocaddress(p_matlib, '_mlxMatlabinc'C)
pr3_matlib = getprocaddress(p_matlib, '_matlibTerminate'C)
! Initialise interface to our Matlab routine
! (this initialisation routine is automatically created by Matlab during compilation)
call matlibInitialize()
a(1) = 1.0
a(2) = 2.0
a(3) = 3.0
a(4) = 5.0
a_mx_p = mxCreateNumericMatrix(4, 1, mxClassIDFromClassName('double'), 0)
a_dx_p = mxGetData(a_mx_p)
call mxCopyReal8ToPtr(a, a_dx_p, 4)
type *, a
call matlabinc(1,b_mx_p, 1,a_mx_p)
b_dx_p = mxGetData(b_mx_p)
call mxCopyPtrToReal8(b_dx_p, b, 4)
type *, b
call mxDestroyArray(a_mx_p)
call mxDestroyArray(b_mx_p)
call matlibTerminate()
end
This is the corresponding MatLab code:
function b = matlabinc(a)
% Increment given argument a and return result b to caller, displaying both on screen as well
disp(a)
b = a + 1;
disp(b)
[/size:645ad5aa41]
Many thanks in advance for any help in that matter.
Benjamin Zimmermann
Edit: I messed up the Code, now it is working