How to transfer parameter between C and fortran
Show older comments
Hello, everyone! Recently, I'm trying to add some source files in c and fortran into my program. And parameters is transfered from matlab to C, then from C to Fortran. After some manipulation, the result will flow from Fortran to C, then to matlab. Before doing this, I did a simple test just to show whther it works, the following is my sourece code in C(.c)
#include "mex.h"
#include <stdio.h>
extern void __stdcall SUB(int *a, int *b);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int nlev = 6;//Layer of the three model
int data = 7;
SUB( &nlev, &data );
}
And the following is the source code in fortran(.F90)
subroutine sub(a, b)
implicit none
integer :: a,b
open(124,file='Dataoutput.txt')
write(124,*) 'The memory allocation part'
write(124,*) a
write(124,*) b
write(124,*) 'The memory allocation finish'
close(124)
end subroutine
If everything goes well, the output would be a file named 'Dataoutput.txt' with the following result:
The memory allocation part
6
7
The memory allocation finish
But the result is not as expected, and the result is as follows:
The memory allocation part
30064771078
4090268226959704071
The memory allocation finish
What's the problem here, can anybody tell me?
Thx a lot!
Accepted Answer
More Answers (0)
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!