Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran and Matlab

nabeels
Beginner
6,630 Views
Hello everybody
Can I anyone tell me how easy to call Fortran by Matlab?
What does intel suppose to send you when you purchase the license?(CDs, manuals, ...)
0 Kudos
28 Replies
IanH
Honored Contributor III
2,686 Views
CVF didn't support C-interoperability, a part of which is the ISO_C_BINDING intrinsic module. Hence you see errors associated with that module and probably also the BIND(C,...) syntax. Not having access to C-interoperability is one of the big downsides to using an old Fortran compiler - C-interoperability really makes things easier and much more robust.

We are also assuming here that you have a version of Matlab that has the necessary support for external dll's. I'm using R2008a. Note again that I don't have CVF installed, so I cannot test my suggestions.

Quoting - nabeels

unfortunately, I have no file withextension .h in my project.

The idea is that you would create it, should you choose to go down this path.

Matlab needs to know a little bit about the functions in the DLL so that it can call them correctly. You can do this in two ways - you can write a C-style header file (*.h) with the function declarations using C syntax (for CVF, *I THINK* the only difference to eos pengwern's example for IVF is the addition of __stdcall and the capitalisation of TEST), or you can provide it with a function handle that returns the information (as per posted example).

So, for CVF, take your original fortran test routine (no BIND(C, ...) etc), compile to a dll.

Header file approach, adapting eos pengwern's instructions. If you know a little C (and how it relates to Fortran) better than you know Matlab this is probably simpler.

Create a file named test.h and put in it:

[cpp]void __stdcall TEST(float *a, float *b, float *c);[/cpp]

Then at the Matlab prompt:

[cpp]loadlibrary('C:path_to_your_dllyour_dll_name.dll', 'test.h')

[new_a, new_b, new_c] = calllib('your_dll_name', 'test', 1.0, 2.0, 3.0)

[/cpp]
Function handle approach as per example m code. This avoids requiring as much knowledge of C, but on the flipside you'll need to know more about Matlab's "internal" description of "external" interfaces, which in R2008a at least, is only documented by example.

I changed my Matlab code to the following after removing my test100 project to the disktop to the following:

clear all
clc
dll_dir = 'C:Documents and SettingsHP_AdministratorDesktoptest100Debug';
dll_path = [dll_dir 'test.dll'];
dll_alias = 'CVFMatlab';
loadlibrary(dll_path, @prototypes, 'alias', dll_alias);

and it still give me an error:

??? Error using ==> loadlibrary at 454
There was an error loading the library "C:Documents and SettingsHP_AdministratorDesktoptest100Debugtest.dll"
Undefined function or variable 'prototypes'.

Error in ==> nabeeltest at 6
loadlibrary(dll_path, @prototypes, 'alias', dll_alias);
Caused by:
Error using ==> feval
Undefined function or variable 'prototypes'.

Any idea??


"prototypes" was an subfunction in the example m code (it could also be a separate function in its own m-file). Matlab can't find it. Two options:

  • To use the example in a self-contained manner take the entire section of matlab code previously posted, copy it into a .m file called CVFMatlab.m and save it in the current Matlab working directory. Then at the matlab prompt type "CVFMatlab". Or
  • To use loadlibrary from the command line or a script (as I think you've been doing) take the section of code from line 32 (function ... prototypes) through to the end of the file, and copy it into a .m file called prototypes.m. Save it in the current Matlab working directory. Then try your script again.
IanH
0 Kudos
nabeels
Beginner
2,686 Views
Quoting - IanH

"prototypes" was an subfunction in the example m code (it could also be a separate function in its own m-file). Matlab can't find it. Two options:

  • To use the example in a self-contained manner take the entire section of matlab code previously posted, copy it into a .m file called CVFMatlab.m and save it in the current Matlab working directory. Then at the matlab prompt type "CVFMatlab". Or
  • To use loadlibrary from the command line or a script (as I think you've been doing) take the section of code from line 32 (function ... prototypes) through to the end of the file, and copy it into a .m file called prototypes.m. Save it in the current Matlab working directory. Then try your script again.
IanH

I created the h file manually and still the error is there:


??? Error using ==> loadlibrary>lFullPath at 525
Could not find file test.h.

Error in ==> loadlibrary at 209
header=lFullPath(header);

Error in ==> nabeeltest at 8
loadlibrary('C:test', 'test.h')

0 Kudos
eos_pengwern
Beginner
2,686 Views
Quoting - nabeels

I created the h file manually and still the error is there:


??? Error using ==> loadlibrary>lFullPath at 525
Could not find file test.h.




Check your Matlab path setting; test.h needs to be in a directory that Matlab can find; I usually put it in the same directory as the .m files. If it's in the same directory as your DLL, and you need to give the full path to your DLL, then you'll need to give the full path to the .h file as well.

Stephen.
0 Kudos
nabeels
Beginner
2,686 Views
Quoting - eos pengwern

Check your Matlab path setting; test.h needs to be in a directory that Matlab can find; I usually put it in the same directory as the .m files. If it's in the same directory as your DLL, and you need to give the full path to your DLL, then you'll need to give the full path to the .h file as well.

Stephen.

It is there, it looks like matlab can't see it
0 Kudos
eos_pengwern
Beginner
2,686 Views
Quoting - nabeels

It is there, it looks like matlab can't see it

What happens if you type 'dir test.h' from the MATLAB command prompt?
0 Kudos
nabeels
Beginner
2,686 Views
Quoting - eos pengwern

What happens if you type 'dir test.h' from the MATLAB command prompt?

>> dir test.h
test.h not found.
>>

Is there another way to call CVF from matlab?
0 Kudos
eos_pengwern
Beginner
2,686 Views
Quoting - nabeels

>> dir test.h
test.h not found.
>>

Is there another way to call CVF from matlab?

If test.h isn't found then the problem must be with the Matlab path setting; have you put test.h in the same directory as your .m files? What filesdo you see if you just type 'dir'?

I really don't know of any other ways to call Fortran from MATLAB, unless you want to try building a COM server (which was supported on some versions of CVF, but I'm not sure which ones). This is a huge subject however, and much more complicated than loading a DLL.

Stephen.
0 Kudos
nabeels
Beginner
2,686 Views
Quoting - eos pengwern

If test.h isn't found then the problem must be with the Matlab path setting; have you put test.h in the same directory as your .m files? What filesdo you see if you just type 'dir'?

I really don't know of any other ways to call Fortran from MATLAB, unless you want to try building a COM server (which was supported on some versions of CVF, but I'm not sure which ones). This is a huge subject however, and much more complicated than loading a DLL.

Stephen.
Hello Stephen
Thank you very much for the help
This is what I got when I ran the command "dir"
>> dir

. Ficks_law_DEHYm.asv Text1.dsp catDYiso.m nabeel.dsw test.h.txt
.. Ficks_law_DEHYm.m Text1.dsw catHYiso.asv nabeel.for testBVP.asv
Catalyst_Pellet.asv Ficks_law_HY.asv Text1.f catHYiso.m nabeel.opt testBVP.m
Catalyst_Pellet.m Ficks_law_HY.m Text1.opt cheb.m nabeel.plg timestwo.f
Catalyst_Pelletm.asv IRCO.asv Text1.plg funCount.asv nabeeltest.asv
Catalyst_Pelletm.m IRCO.m bvp4cfsinghouseqr.m funCount.m nabeeltest.m
Debug IRCOFD.asv bvp5c2.m hs_err_pid1524.log reactor_par.asv
Ficks_law.asv IRCOFD.m catDY.asv main2.asv reactor_par.m
Ficks_law_DEHY.asv MassDYiso.asv catDY.m main2.m sparse2.mexw32
Ficks_law_DEHY.m MassDYiso.m catDYiso.asv nabeel.dsp test.f

I can see the text file I created as test.h.txt, can I discuss it through e-mail?
My e-mail is: naboghander@gmail.com , if you like
0 Kudos
Reply