Call a Fortran code in Matlab to get the result which I will use it in Matlab code

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

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 ...?
It is Fortran executable and output from Fortran code will be a data file which will be changed by iterative procedure until it will be converged
Please show you system() command
I am doing Fortran on visual studio. When I am using system('path') it only opens the interface nothing else . After that I have to run it also and give some input like options
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?
I have the files can I send it to you? Do you have Fortran and Matlab installed? In case you don't have we can connect through Team Viewer.
Please open this zip file and run 'communication.sln'.
system('C:\Users\Sumit Pal\Desktop\Test\communicationtest\communicationtest.sln')
N.B From fortran code the value of 'f' come and it will go into matlab function and the value of the matlab function is 'V' which will again go into Fortran code and give the result and go into Matlab.....
when I'm using system command, it is opening only the interface like the image below :
Sumit Pal
Sumit Pal am 28 Aug. 2015
Bearbeitet: Sumit Pal am 28 Aug. 2015
Okay got it. I am editing it. Please help me to solve the problem. have you checked it ?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

When trying to run this I'm getting following error. What to do? :-(
!msbuild "C:\Users\Sumit Pal\Desktop\Test\communicationtest\communicationtest.sln"
'msbuild' is not recognized as an internal or external command, operable program or batch file.

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.

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');
V=f*sin(10); % 'f' will come from fortran code
% ... do something here ...
fclose(rfid);
Finally I'm doing this code but when running I'm getting a error (attached image- capture)
Also fortran is not running.
result = system( sprintf('"%s" < %s', executable, tinfile) );
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.
I'm getting error in fread.
It's invalid file identifier.
Ultimately it's not giving error when I use 'main.f90' instead of previous one.... but it is running for inf time (though I've checked only 2 hours, for that time it was busy)..... I don't know what is happening ... Could you please explain me ? Please
Another thing is that it is not getting any manual input (because my fortran code needs some manual input in the very first step after that it will run iteratively)...
e.g.step-1 I will input the number of iterations (which I denoted as n) step-2 I will give input V=0 so the f will be 10 step-3 this f=10 value will be put into matlab equation suppose this value V=x step-4 this V=x value will again go to fortran code and give a value of f .........
by this it will run up to 'n' which I have to specify at the very 1st stage.
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.
Then I will call only the subroutine 'timestwo.f90' from Fortran and get the value of 'f'. Could you please tell me in detail how to do it
subroutine timestwo(f, V)
real*8 f, V
f = 10 - 10*V +10* V**2
return
end
Thanks for this. But I've seen it earlier and also trying to do but didn't get the steps properly. Could you please make it easier for me? It will be very helpful.
No, I cannot make it easier for you. There is a complete working example there. You can modify that code as you need.
When I'm trying to create 'mex timestwo.f90 ' file it's giving this error even when I've installed SDK 7.1 . I also searched over the internet and found this problem happened for many users also but ultimately I didn't find the answer till now. Need your help. Please help me.
>> mex timestwo.f90
Error using mex
No supported compiler or SDK was found. For options, visit
http://www.mathworks.com/support/compilers/R2014a/win64.
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.
I am working on windows 7 and I installed also SDK 7.1 but giving me the above error message .... Is there anything along with it that I have to install so that it will work?
I'm trying to install any of the two that you have mentioned above.
You need the fortran compiler. After that use
mex -setup Fortran
Sumit Pal
Sumit Pal am 31 Aug. 2015
Bearbeitet: Sumit Pal am 31 Aug. 2015
I installed Intel Visual Fortran Composer XE 2013 from the link and as I said also installed SDK 7.1.
When I am using 'mex -setup Fortran' this is giving the same error like previous, I'm really upset.
That link is only the Run-time library, not the compiler.
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.
If you have time to play around, on MS Windows you could install g95 for Windows and see if you can get it to work; for that you would likely be interested in gnumex
Yes, I am a student. I installed Intel Visual Fortran Composer XE 2013 on my pc and also generated the mex file but this is showing an error. There is any thing missing again?
previous problem is solved by changing mxCreateDoubleMatrix instead of mxCreateFull but a new problem is coming.
Please look at it.
MATLAB crash file:C:\Users\RANADE~1\AppData\Local\Temp\matlab_crash_dump.2288-1:
------------------------------------------------------------------------
Access violation detected at Wed Sep 02 19:56:04 2015
------------------------------------------------------------------------
Configuration:
Crash Decoding : Disabled
Default Encoding : windows-1252
MATLAB Architecture: win64
MATLAB Root : C:\Program Files\MATLAB\R2014a
MATLAB Version : 8.3.0.532 (R2014a)
Operating System : Microsoft Windows 7 Professional
Processor ID : x86 Family 6 Model 60 Stepping 3, GenuineIntel
Virtual Machine : Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Window System : Version 6.1 (Build 7601: Service Pack 1)
Fault Count: 1
Abnormal termination:
Access violation
Register State (from fault):
RAX = 0000000000000008 RBX = 000000000402af40
RCX = 000000000402a030 RDX = 766d9fc071d03790
RSP = 000000000402a008 RBP = 000000000402b060
RSI = 000000000402b060 RDI = 000000000402af40
R8 = 0000000000000008 R9 = 0000000000000001
R10 = 0000000000000001 R11 = 000000000402a030
R12 = 000000000000127f R13 = 00000000686d6ed0
R14 = 0000000000000001 R15 = 000000000402b060
RIP = 0000000072e9bfbf EFL = 00010202
CS = 0033 FS = 0053 GS = 002b
Stack Trace (from fault):
[ 0] 0x0000000072e9bfbf C:\Program Files\MATLAB\R2014a\bin\win64\MSVCR100.dll+00245695 memmove+00000095
[ 1] 0x000007fef92f10ae C:\Users\Ranadev Dutta\Desktop\Test\communicationtest\timestwo.mexw64+00004270 MEXFUNCTION+00000174
[ 2] 0x00000000fc5ef700 C:\Program Files\MATLAB\R2014a\bin\win64\libmex.dll+00063232 mexRunMexFile+00000112
[ 3] 0x00000000fc5ee1e2 C:\Program Files\MATLAB\R2014a\bin\win64\libmex.dll+00057826 inSwapMexfileReader+00000226
[ 4] 0x00000000fc5ef294 C:\Program Files\MATLAB\R2014a\bin\win64\libmex.dll+00062100 _constructor_lm_mexmaci64+00001620
[ 5] 0x000000000458be4f C:\Program Files\MATLAB\R2014a\bin\win64\m_dispatcher.dll+00048719 Mfh_file::dispatch_fh+00000687
[ 6] 0x000000000458c77a C:\Program Files\MATLAB\R2014a\bin\win64\m_dispatcher.dll+00051066 Mfunction_handle::dispatch+00000810
[ 7] 0x0000000004705e04 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00744964 MathWorks::MException::MExceptionEnvelope::GetID+00539764
[ 8] 0x00000000046f0782 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00657282 MathWorks::MException::MExceptionEnvelope::GetID+00452082
[ 9] 0x00000000046e3f6f C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00606063 MathWorks::MException::MExceptionEnvelope::GetID+00400863
[ 10] 0x00000000046ccd43 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00511299 MathWorks::MException::MExceptionEnvelope::GetID+00306099
[ 11] 0x00000000046d644f C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00549967 MathWorks::MException::MExceptionEnvelope::GetID+00344767
[ 12] 0x00000000046e4962 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00608610 MathWorks::MException::MExceptionEnvelope::GetID+00403410
[ 13] 0x00000000046e192c C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00596268 MathWorks::MException::MExceptionEnvelope::GetID+00391068
[ 14] 0x00000000046f11c9 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+00659913 MathWorks::MException::MExceptionEnvelope::GetID+00454713
[ 15] 0x000000000458c00e C:\Program Files\MATLAB\R2014a\bin\win64\m_dispatcher.dll+00049166 Mfh_file::dispatch_fh+00001134
[ 16] 0x000000000458c77a C:\Program Files\MATLAB\R2014a\bin\win64\m_dispatcher.dll+00051066 Mfunction_handle::dispatch+00000810
[ 17] 0x0000000004764a0e C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+01133070 inValidateLoadedObject+00003486
[ 18] 0x000000000476bd95 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+01162645 inResetInterpreter+00005029
[ 19] 0x0000000004770a05 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+01182213 MathWorks::MException::MExceptionEnvelope::Copy+00010997
[ 20] 0x00000000047709d2 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+01182162 MathWorks::MException::MExceptionEnvelope::Copy+00010946
[ 21] 0x0000000004770997 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+01182103 MathWorks::MException::MExceptionEnvelope::Copy+00010887
[ 22] 0x0000000004b5e89c C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+05302428 inMexPutVariable+00004204
[ 23] 0x0000000004b5ebf6 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+05303286 inMexPutVariable+00005062
[ 24] 0x0000000004b5f02e C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+05304366 inMexPutVariable+00006142
[ 25] 0x0000000004b5f36c C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+05305196 inMexPutVariable+00006972
[ 26] 0x0000000004a70161 C:\Program Files\MATLAB\R2014a\bin\win64\m_interpreter.dll+04325729 inEvalCmdWithLocalReturn+00000065
[ 27] 0x00000000050cbecd C:\Program Files\MATLAB\R2014a\bin\win64\libmwbridge.dll+00048845 mnDebugPrompt+00000109
[ 28] 0x00000000050ccfaf C:\Program Files\MATLAB\R2014a\bin\win64\libmwbridge.dll+00053167 mnParser+00000735
[ 29] 0x00000000fd0afae4 C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00195300 mcrInstance::mnParser_on_interpreter_thread+00000036
[ 30] 0x00000000fd093af0 C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00080624 mcr::runtime::setInterpreterThreadToCurrent+00029488
[ 31] 0x00000000fd093b50 C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00080720 mcr::runtime::setInterpreterThreadToCurrent+00029584
[ 32] 0x00000000fab6e026 C:\Program Files\MATLAB\R2014a\bin\win64\uiw.dll+00581670 UIW_IsUserMessage+00000086
[ 33] 0x00000000fab6e705 C:\Program Files\MATLAB\R2014a\bin\win64\uiw.dll+00583429 UIW_SetGLIMUserMsg+00000117
[ 34] 0x00000000fab6e789 C:\Program Files\MATLAB\R2014a\bin\win64\uiw.dll+00583561 UIW_SetGLIMUserMsg+00000249
[ 35] 0x00000000773d87fe C:\Windows\system32\USER32.dll+00165886 GetMenuBarInfo+00000638
[ 36] 0x00000000773bf5fb C:\Windows\system32\USER32.dll+00062971 SystemParametersInfoW+00000235
[ 37] 0x00000000773c4895 C:\Windows\system32\USER32.dll+00084117 IsProcessDPIAware+00000465
[ 38] 0x000000007761dae5 C:\Windows\SYSTEM32\ntdll.dll+00318181 KiUserCallbackDispatcher+00000031
[ 39] 0x00000000773c908a C:\Windows\system32\USER32.dll+00102538 PeekMessageW+00000186
[ 40] 0x00000000773c50fe C:\Windows\system32\USER32.dll+00086270 GetKeyState+00000238
[ 41] 0x00000000773c3a6f C:\Windows\system32\USER32.dll+00080495 PeekMessageA+00000087
[ 42] 0x00000000fab1bd4b C:\Program Files\MATLAB\R2014a\bin\win64\uiw.dll+00245067 UIW_SetCurrentDialog+00000731
[ 43] 0x00000000fab6fbe2 C:\Program Files\MATLAB\R2014a\bin\win64\uiw.dll+00588770 ws_FreeSystemFont+00000482
[ 44] 0x00000001000e2ed0 C:\Program Files\MATLAB\R2014a\bin\win64\libmwservices.dll+00995024 services::system_events::PpeDispatchHook::~PpeDispatchHook+00053488
[ 45] 0x00000001000e36a5 C:\Program Files\MATLAB\R2014a\bin\win64\libmwservices.dll+00997029 services::system_events::PpeDispatchHook::~PpeDispatchHook+00055493
[ 46] 0x00000001000e4330 C:\Program Files\MATLAB\R2014a\bin\win64\libmwservices.dll+01000240 services::system_events::PpeDispatchHook::~PpeDispatchHook+00058704
[ 47] 0x00000001000ed4c6 C:\Program Files\MATLAB\R2014a\bin\win64\libmwservices.dll+01037510 sysq::removeProcessPendingEventsHook+00011030
[ 48] 0x00000001000ed7ca C:\Program Files\MATLAB\R2014a\bin\win64\libmwservices.dll+01038282 svWS_ProcessPendingEvents+00000138
[ 49] 0x00000000fd094b6d C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00084845 mcr::runtime::setInterpreterThreadToCurrent+00033709
[ 50] 0x00000000fd094e3b C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00085563 mcr::runtime::setInterpreterThreadToCurrent+00034427
[ 51] 0x00000000fd088b75 C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00035701 mcrInstantiationError::operator=+00003221
[ 52] 0x00000000fd08bea2 C:\Program Files\MATLAB\R2014a\bin\win64\mcr.dll+00048802 mcr_invoke_on_interpreter_thread+00001602
[ 53] 0x00000000fa7e6e5d C:\Program Files\MATLAB\R2014a\bin\win64\MVMLocal.dll+00159325 mvm_server::inproc::LocalFactory::createMVM+00040653
[ 54] 0x00000000051bafaa C:\Program Files\MATLAB\R2014a\bin\win64\mvm.dll+00569258 mvm::detail::SessionImpl::privateSession+00001754
[ 55] 0x00000000051bd32a C:\Program Files\MATLAB\R2014a\bin\win64\mvm.dll+00578346 mvm::detail::SessionImpl::privateSession+00010842
[ 56] 0x00000000051bce85 C:\Program Files\MATLAB\R2014a\bin\win64\mvm.dll+00577157 mvm::detail::SessionImpl::privateSession+00009653
[ 57] 0x0000000140004f79 C:\Program Files\MATLAB\R2014a\bin\win64\matlab.exe+00020345
[ 58] 0x0000000140006a07 C:\Program Files\MATLAB\R2014a\bin\win64\matlab.exe+00027143
[ 59] 0x00000000774c59cd C:\Windows\system32\kernel32.dll+00088525 BaseThreadInitThunk+00000013
[ 60] 0x00000000775fb981 C:\Windows\SYSTEM32\ntdll.dll+00178561 RtlUserThreadStart+00000033
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
If this problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/
A technical support engineer might contact you with further information.
Thank you for your help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Compiler finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 27 Aug. 2015

Kommentiert:

am 2 Sep. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by