Mex files and Lapack
The mex files allow you to call C and Fortran subroutines from Matlab like you would with functions. Here we will concentrate on Fortran subroutines.A call to an mex file named "start", i.e, [C,D]=start(A,B) allows Matlab to pass variables A,B to the mex file. C and D are not assigned.
Input:
integer B: B=prhs(2)Use mxget to take data from prhs(1),(2),...
integer A: A=prhs(1)
Gateway routine:In the gateway routine, use the mxcreate function to form arrays for output arguments. Set plhs(1),(2),...to the new Matlab arrays. The entry to the gateway should have the name mexfunction, with the following parameters:
subroutine mexfunction(nlhs,plhs,nrhs,prhs) integer plhs(*),prhs(*) integer nlhs, nrhsCall the Fortran subroutine, passing input and output data pointers as function parameters.Output:
From the mex-file start, [C,D]=start(A,B) plhs(1)is assigned to C, plhs(2) to DintegerD,D=plhs(2)
integer C, C=plhs(1)
Here is a complete piece of code:
In the following, xsgesvg.f is the header file. It calls xsgesv.f, which in turn calls a driver from the LAPACK library that solves a system of linear equations using LU decomposition with pivoting.
The header file:
For
Click on xsgesvg.f to view the code. Inside the program will be a call towards the bottom, CALL XSGESV(RYPP,RTP,RYP). This is the call to xsgesvg.f.mexfiles, matlab requires a header so that the program can be compiled.The sgesv.f file:
The xsgesv.f file:
Click on xsgesv.f to view the code.Inside the program will be a call, call SGESV( N, NRHS, A, LDA, IPIV, B, LDB, INFO ). This calls the LAPACK subroutine SGESV that solves Ax=b.
Click on sgesv.f to view the code.Inbetween all this is code that basicaly allows the programs to compile into onemexexecutable file.
Other examples concerning Matlab and Fortran, as well as C, can be found in the directory:
/apps/matlab5.0/extern/examples/
