Call a Fortran code in Matlab to get the result which I will use it in Matlab code
Ältere Kommentare anzeigen
I have a Fortran code and I want to run it in MATLAB, By this I will get the output from Fortran code and will use it in Matlab code. I was trying with system command but it is only opening Fortran interface with code not running.Please help me to do this.
9 Kommentare
James Tursa
am 27 Aug. 2015
You have Fortran source code, or a Fortran executable? Is the output from the Fortran code a data file, or a set of variables you want imported into MATLAB, or ...?
Sumit Pal
am 27 Aug. 2015
Walter Roberson
am 28 Aug. 2015
Please show you system() command
Sumit Pal
am 28 Aug. 2015
Walter Roberson
am 28 Aug. 2015
Please show your system() command -- I need to see the exact command you are using.
Which interface is being opened?
How do you "give some input like options" ?
Are you indicating that you need your MATLAB code to take the step of compiling the fortran to an executable and then running the executable?
Sumit Pal
am 28 Aug. 2015
Sumit Pal
am 28 Aug. 2015
Bearbeitet: Walter Roberson
am 28 Aug. 2015
Walter Roberson
am 28 Aug. 2015
Antworten (1)
Walter Roberson
am 28 Aug. 2015
Once, not every time, you should execute
!msbuild "C:\Users\Sumit Pal\Desktop\Test\communicationtest\communicationtest.sln"
after that the code will be in compiled form and you will be able to system() the .exe, whatever it got named. For example it might be
system('C:\Users\Sumit Pal\Desktop\Test\communicationtest\main.exe')
You can pass input to the .exe by building an input file in MATLAB and redirecting input from the file. For example,
tinfile = tempname();
fid = fopen(tinfile, 'wt');
fprintf(fid, '%d\n', 3);
fprintf(fid, '%f\n', [7, 31, pi]);
fclose(fid);
executable = 'C:\Users\Sumit Pal\Desktop\Test\communicationtest\main.exe';
result = system( sprintf('%s < %s', executable, tinfile) );
resfile = 'out_plot.dat';
rfid = fopen(resfile, 'r');
... do something here ...
fclose(rfid);
At the moment I do not have the resources to investigate the format that the
write(20,*) V,f
is going to use in the file. I do not know if it would be text or if it would be binary. Try
data = fread(rfid);
as I suspect that might work.
22 Kommentare
Sumit Pal
am 28 Aug. 2015
Walter Roberson
am 28 Aug. 2015
The information I find indicates that msbuild is used internally and must always be present for Visual Studio to work. It might not be on you MS Windows path, though.
I could not tell which version of Visual Studio you are using. If you are using a version before 2013 then msbuild is part of the .NET platform. See http://blogs.msdn.com/b/visualstudio/archive/2013/07/24/msbuild-is-now-part-of-visual-studio.aspx
See also https://social.msdn.microsoft.com/forums/windowsapps/en-us/23a7dc5d-c337-4eed-8af4-c016def5516e/location-of-msbuildexe for specific places that msbuild might be.
Sumit Pal
am 28 Aug. 2015
Bearbeitet: Walter Roberson
am 28 Aug. 2015
Walter Roberson
am 28 Aug. 2015
result = system( sprintf('"%s" < %s', executable, tinfile) );
Walter Roberson
am 28 Aug. 2015
Before your line
V=f*sin(10);
insert
data = fread(rfid);
data = reshape(data,2,[]);
f = data(2,:);
I am guessing here about how the data needs to be read.
Sumit Pal
am 28 Aug. 2015
Sumit Pal
am 28 Aug. 2015
Sumit Pal
am 28 Aug. 2015
Walter Roberson
am 28 Aug. 2015
You cannot do interactive I/O between Fortran and MATLAB except by opening a pipe . The code there is designed for Unix but in theory MS Windows from XP SP3 onwards has popen; if not then it has _popen. Pipes are not part of MATLAB.
What would be supported is if you change your code into a subroutine, compile it to a dll, and loadlibrary() it, after which you could call your routine passing in arguments and getting back results.
Sumit Pal
am 29 Aug. 2015
Sumit Pal
am 31 Aug. 2015
Walter Roberson
am 31 Aug. 2015
No, I cannot make it easier for you. There is a complete working example there. You can modify that code as you need.
Sumit Pal
am 31 Aug. 2015
Walter Roberson
am 31 Aug. 2015
You need to install a fortran compiler. You need Intel Visual Fortran Composer XE 2013 or Intel Visual Fortran Composer XE 2011 . You will also need that SDK 7.1
Note: if you are running Windows 10 then you might not be able to get anything to work.
Sumit Pal
am 31 Aug. 2015
Walter Roberson
am 31 Aug. 2015
You need the fortran compiler. After that use
mex -setup Fortran
Sumit Pal
am 31 Aug. 2015
Bearbeitet: Walter Roberson
am 31 Aug. 2015
Walter Roberson
am 31 Aug. 2015
That link is only the Run-time library, not the compiler.
Are you a student? If so then see https://software.intel.com/en-us/qualify-for-free-software/student
Otherwise see http://softwarestore.ispfulfillment.com/Store/Product.aspx?skupart=I23SE1 for the first part listed there, "Intel® Parallel Studio XE Composer Edition for Fortran Windows - Named-user Commercial [Electronic Delivery]". For me it shows up as $US849 but perhaps you could find it at a different price.
No, there are no supported free Fortran compilers for MS Windows, at least for non-students. You may wish to consider dual-booting Linux as gfortran is supported for that.
Sumit Pal
am 2 Sep. 2015
Sumit Pal
am 2 Sep. 2015
Kategorien
Mehr zu MATLAB Compiler finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






