How to pass arguments by reference from Matlab?

조회 수: 3 (최근 30일)
Si Soso
Si Soso 2020년 7월 3일
댓글: James Tursa 2020년 7월 3일
Hi,
I have a Fortran Dll which has a subroutine with some arguments defined by reference (Attributes Reference). I need to call the subroutine from Matlab and pass different type of arguments: reals, integers, logical and characters. My questions are as follows:
1- Can Matlab pass arguments by reference (either to a Matlab function or a function defined in another language like Fortran)?
2- How can one call the dll from Matlab if several functions and subroutines are within the dll file?
3- Can Matlab pass arguments that are logical (booleans) and a mix of data?
4- Is there any documentation that explains all this?
Thanks,
Soso

답변 (1개)

James Tursa
James Tursa 2020년 7월 3일
doc loadlibrary
Create a C header file that gives prototypes for the Fortran subroutines and treat the Fortran arguments as pointers.
E.g., if the Fortran subroutine was this:
subroutine mysub(d,s,i)
real*8 d
real*4 s
integer*4 i
then the C header file would contain
void MYSUB(double *, float *, int *);
The uppercase is because the Fortran compiler may turn the lowercase name into uppercase.
The character string arguments are going to be tricky, however, since Fortran passes the length of the string silenly by value in the background. Could either be at the beginning or the end.
What exact subroutine signatures are you dealing with?
  댓글 수: 2
Si Soso
Si Soso 2020년 7월 3일
Hi James,
Thank you for your response. The Fortran subroutine is like this one (please note that here, the character and logical variables are not there):
-----------------------------------------
module foo
contains
subroutine foof(c,a,b,n,m)
!DEC$ ATTRIBUTES DLLEXPORT :: foof
!DEC$ ATTRIBUTES VALUE :: b,n,m
!DEC$ ATTRIBUTES REFERENCE :: a,c
integer, INTENT(IN) :: n,m
double precision , INTENT(IN) :: a(*),b
double precision , INTENT(OUT) :: c(*)
do 10 i=1,m*n
c(i) = sin(a(i))+b
10 continue
end subroutine
end module foo
--------------------------
James Tursa
James Tursa 2020년 7월 3일
The C header file would look something like this:
/* File name foof.h */
void foof( double *c, double *a, double b, int n, int m );
The caveat is that foof might need to be FOOF if the compiler turns it into uppercase. Also, the above assumes the default integer for Fortran is a 32-bit integer. But if you are compiling such that the Fortran default integer is 64-bit, then the prototype in the header file would be this instead:
void foof( double *c, double *a, double b, long long n, long long m );
Then you feed 'foof.h' to loadlibrary.
The general rule is that arguments passed by reference in Fortran are passed as pointers in C (i.e., with the asterisk), and arguments passed by value in Fortran are not (i.e., without the asterisk).
Logical arguments in Fortran are probably 1-byte, in which case you would use char in the C prototype. But if they happen to be 4-byte, then you would use int on the C side.
Char string arguments in Fortran would be passed as pointer-to-char on the C side, but there is probably a string length passed silently by value as well. Could be at the beginning or at the end or right next to the character string argument ... depends on the compiler and you will have to experiment to see what works. E.g., a Fortran subroutine such as this
subroutine foo(a,c,b)
real*8, intent (in) :: a, b
character*(*), intent(in) :: c
Could be any of the following depending on the compiler:
void foo( double *a, char *c, double *b, int len );
void foo( double *a, char *c, int len, double *b );
void foo( double *a, int len, char *c, double *b );
void foo( int len, double *a, char *c, double *b );
And that int may need to be a long long type instead.
I haven't looked at this recently so don't know what the current compilers do. I know the old Compaq compiler did it differently than the Intel compiler, I just don't remember which way they did it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by