I would like to call MATLAB from my Fortran application using the MATLAB Engine interface. However, I would like to compile my project in the Microsoft Visual Studio 2008 environment and not in MATLAB 7.10 (R2010a).
MATLAB: How to compile a MATLAB Engine Fortran application using Microsoft Visual Studio 2008, Intel Visual Fortran 11.1 and MATLAB 7.10 (R2010a)
enginefortranintelMATLABstudiovisual
Related Solutions
To compile FORTRAN code into a MEX file with Intel Fortran Composer XE 2011 in Microsoft Visual Studio 2010, follow the ensuing steps.
This solution demonstrates compiling the example file 'timestwo.F'.
1. Set up a project in Visual Studio as follows:
a. Open Microsoft Visual Studio and navigate to 'File' -> 'New' -> 'Project'.
b. Select 'Intel Visual Fortran' -> 'Library' -> 'Dynamic-link Library'.
c. Following this, enter the name of the project and its location in the indicated text areas and click OK.
2. Add 'timestwo.F' to the project:
a. Copy the file 'timestwo.F' to the folder where you created the Visual Studio project. This file may be obtained from the following location:
$MATLABROOT\extern\examples\refbook
where $MATLABROOT is the MATLAB root directory, and may be determined by entering the command:
>> matlabroot
at the MATLAB command prompt.
b. In Visual Studio, right-click on the 'Source Files' folder in the 'Solution Explorer' and click 'Add' -> 'Existing Item...'.
c. Select 'timestwo.F' and click 'Add'.
3. Create the module definition file:
a. Right click on the project name, choose 'Add' -> 'New Item'.
b. Select 'Text File (.txt)'
c. Name the file 'module.def' (remove the .txt extension).
d. After creating it, the file should open in Visual Studio. Paste the following lines:
LIBRARY MYFILE EXPORTS MEXFUNCTION
where MYFILE is the name of the MEX file to create, e.g. timestwo.mexw32
e. Save the file.
4. Right click on the project name and select 'Properties' in the solution explorer to open the project
properties. Once this window has opened, make the following changes:
a. Under 'Fortran' -> 'General', add the directory
$MATLABROOT\extern\include
to the field 'Additional Include Directories'.
b. Under 'Fortran' -> 'Preprocessor', choose 'Yes(/fpp)' for 'Preprocess Source File'.
c. Also under 'Fortran' -> 'Preprocessor', add the following to 'Preprocessor Definitions':
MATLAB_MEX_FILE
d. Under 'Fortran' -> 'External Procedures', set the 'Calling Convention' field to:
'C, REFERENCE (/iface:cref)'
e. Also under 'Fortran' -> 'External Procedures', set the 'Name Case Interpretation' field to:
'Upper Case (/names:uppercase)'.
f. Under 'Linker' -> 'General', change 'Output File' to '$(OutDir)\$(ProjectName).mexw32'.
g. Also under 'Linker' -> 'General', add the directory
$MATLABROOT\extern\lib\win32\microsoft
to the 'Additional Library Directories' field.
h. Under 'Linker' -> 'Input', add the following:
libmx.lib libmex.lib libmat.lib
to the 'Additional Dependencies' field.
i. Also under 'Linker' -> 'Input', add the file
module.def
to the 'Module Definition File' field.
j. Click 'Ok' to save and close the project properties.
e. Click 'Apply' -> 'Ok' to close the project properties.
5. Build the solution.
6. In MATLAB, change the current MATLAB directory to the directory in which the MEX file was
created.
7. Run the MEX file using the following command:
<mexfilename>(inputs)
For instance, if you built 'timestwo' into a MEX file named 'timestwo.mexw32' run it as follows:
>> timestwo(2.3)
You should receive the following output:
4.600000
The following example was compiled in Visual Studio 2005 with Intel Fortran 9.1 integrated, and with example files from MATLAB 7.4 (R2007a). The steps are similar to the ones described in the documentation at
web([docroot,'/techdoc/matlab_external/f24338.html'])
Note that in the following lines MATLABROOT refers to the MATLAB install directory on your machine.
1. Create a project in Visual Studio 2005 of type Intel Fortran Project -> Dynamic Link Library, and name it for example yprime_fortran.
2. Insert your MEX source files (the example here uses the files yprimef.F and yprimefg.F from MATLABROOT\extern\examples\mex).
3. Create a .def file to export the MEX entry point. On the Project menu, click Add New Item and select Text File. Name the file module.def and include the following text
LIBRARY yprime_fortran.mexw32EXPORTS _MEXFUNCTION@16
4. On the Project menu, click Properties for the project and make the following changes:
a. Under Fortran -> General -> Additional Include Directories, add the MATLAB include directory MATLABROOT\extern\include
b. Under Fortran -> General -> Preprocessor Definitions, add MATLAB_MEX_FILE as a preprocessor definition.
c. Under Fortran -> Preprocessor -> Preprocess Source File, choose Yes
d. Under Fortran -> External Procedures -> Calling Convention, choose 'STDCALL, REFERENCE'
e. Under Fortran -> External Procedures -> Name Case Interpretation, choose Upper Case
f. Under Linker -> General, change the output file extension to .mexw32 if you are building for a 32–bit platform or .mexw64 if you are building for a 64–bit platform.
g. Locate the .lib files for the compiler you are using under MATLABROOT\extern\lib\win32\microsoft or MATLABROOT\extern\lib\win64\microsoft. Under Linker -> Input -> Additional Dependencies, add libmx.lib and libmex.lib.
h. Under Linker -> Input -> Module Definition File, add the module definition (.def) file you created.
i. Under Debugging -> Command, copy the path to MATLAB.exe (for example, C:\Program Files\MATLAB\R2007a\bin\win32\MATLAB.exe)
5. Build the solution and run it using Debug -> Start Debugging. This will open a MATLAB window
6. Change to the directory containing the .mexw32 function and type
>> which yprime_fortran
Ensure that this points to the current directory.
7. Execute
>> yprime_fortran(1,1:4)
and you should see a result similar to the following
ans = 2.0000 8.9685 4.0000 -1.0947
The attached ZIP-file contains all the files including the Visual Studio project (except for the 2 .F files available in MATLABROOT\extern\examples\mex). You might need to change the directory structures to point to your files, as well as delete and add the source .F files from the correct location.
Related Question
- How to compile matcreat.cpp demo using Microsoft Visual Studio Express 2010
- How to debug a Fortran MEX file on Windows using Intel, Compaq, or Digial Visual Fortran compilers with Microsoft Visual Studio
- How to call/use the MATLAB engine from a Fortran program
- How to compile an Engine application that uses MATLAB 7.0 (R14) in Visual Studio .NET 2003
- Mex -setup -v FORTRAN, Failure using R2014a with Win7, MSVS2013 sp1 and Intel Fotran Composer 2013 SP1
- How to debug a MATLAB 7.1 (R14SP3) MEX-file in Microsoft Visual Studio 6.0
Best Answer