How to call MATLAB function from Fortran 77 subroutine

Hello, I want fortran subroutine to call simple matlab function (.m) file during execution. The output of matlab function is stored in fortran subroutine. I have prepared following codes: 1. is Fortran 77 subroutine 2. is Matlab program. I want to know how to call matlab program from fortran 77 subroutine and exchange data. Any input will be appreciated: Fortran code:
program Frotran_Learning_1
implicit none
integer :: a
real :: c,b
write (*,*) 'Enter the values for a & b:'
read (*,*) a,b
c = product(a,b)
write (*,*) 'The product of a and b is: '
write (*,*) c
end program Frotran_Learning_1
MATLAB Code:
function [ z ] = product( x,y )
%multiplies x and y
z=x*y;
end
Can anybody please tell me how to inteface this two? Thank you in advance,
Nik

Réponses (1)

James Tursa
James Tursa le 26 Avr 2017

0 votes

See the External Interfaces API section of the doc. The Engine would be one option. Or rewriting your Fortran as a mex routine is another option.
https://www.mathworks.com/help/matlab/programming-interfaces-for-c-c-fortran-com.html

4 commentaires

Hello, Thanks for the link. It looks very complicated. What I am looking for is very simple. How to call matlab function from fortran subroutine and store the output of matlab function in fortran subroutine. Thats it. Can you please tell me if there is any simple way to do this? Thank you in advance,
The engine is not suitable option because I have separate fortran compiler which is invoked from abaqus. Therefore, I would prefer fortran subroutine to start matlab execute the .m file and get the output for further processing.
Any idea whats going wrong in following code?
program Frotran_Learning_1
implicit none
#include "fintrf.h"
integer :: a
integer :: c,b
write (*,*) 'Enter the values for a & b:'
read (*,*) a,b
integer*4 mexCallMATLAB(nlhs, plhs, nrhs, prhs, product)
integer*4 nlhs, nrhs
mwPointer plhs(*)=c, prhs(*)=a
character*(*) product
c=plhs
!c = product(a,b)
write (*,*) 'The product of a and b is: '
write (*,*) c
end program Frotran_Learning_1
Matlab code:
function [ z ] = product( x )
%multiplies x and y
z=x*x;
end
James Tursa
James Tursa le 26 Avr 2017
Modifié(e) : James Tursa le 26 Avr 2017
"... I have separate fortran compiler which is invoked from abaqus ..."
So does this mean you are using a Fortran compiler that is not officially supported by MATLAB?
It is not too difficult to write Fortran source code to call an m-file, and I can certainly help with that, but first you need to be able to build Engine or mex routines. That requires either using a supported Fortran compiler, or somehow finding 3rd party instructions for compiling & linking your particular compiler with MATLAB. Have you done this?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Fortran with MATLAB dans Centre d'aide et File Exchange

Tags

Question posée :

le 26 Avr 2017

Modifié(e) :

le 26 Avr 2017

Community Treasure Hunt

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

Start Hunting!

Translated by