Efficiently call MATLAB code from another coding language
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I am trying to run a MATLAB code as a simulation within an optimisation framework to solve for the responses. The optimisaiton is performed using Intel Fortran in Visual Studio.
I am currently doing this via calling a batch (.bat) file that opens and runs a MATLAB .m file with a specified path:
matlab -noFigureWindows -nosplash -nodesktop -r "try, run('C:\Users\path\file.m'), catch, exit, end, exit"
However, this is opens and closes MATLAB for every simulation performed, which is quite inefficient.
I wondered if there is a better way?
I have had ideas of using MATLAB compiler to build a .exe application, or potentially directly calling MATLAB from Visual Studio Fortran code as if it were a subroutine, but am unsure if this is possible. I'd very much appriciate any advice/info on what is most efficient and how to achieve this.
Thank you
Edit -
I am currently using the MATLAB PDE toolbox in my code.
0 个评论
采纳的回答
Walter Roberson
2021-3-29
If you can compile your file.m into a function, then it is possible to use MATLAB Compiler SDK to create a DLL that could be called from Fortran.
However, that DLL would effectively have to start the MATLAB interpreter each time, unless you had MATLAB Production Server installed. It would not, though, require that a licensed copy of MATLAB be present at runtime.
If you can compile your file.m using MATLAB Coder, you can generate C or C++ code that you can compile into a library that could be called from Fortran, and in this situation it would not start up MATLAB each time. Unfortunately, MATLAB Coder is only able to compile a portion of MATLAB -- no graphics for example. It would not, though, require that a licened copy of MATLAB be present at runtime.
A third possibility is to use Automation Server https://www.mathworks.com/help/matlab/call-matlab-com-automation-server.html to call into MATLAB using Active-X type calls. This can run most of MATLAB, and would not require that MATLAB be started each time (as long as the server process was still around.) It would, however, require that a licensed copy of MATLAB be present at runtime.
3 个评论
Walter Roberson
2021-3-29
Yes, you are correct: PDE is supported for MATLAB Compiler (and MATLAB Compiler SDK), but not MATLAB Coder, it appears. It would be usable from Automation Server.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Troubleshooting in MATLAB Compiler SDK 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!