Efficiently call MATLAB code from another coding language
2 views (last 30 days)
Show older comments
Elliot Bontoft
on 29 Mar 2021
Commented: Elliot Bontoft
on 29 Mar 2021
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 Comments
Accepted Answer
Walter Roberson
on 29 Mar 2021
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 Comments
Walter Roberson
on 29 Mar 2021
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.
More Answers (0)
See Also
Categories
Find more on Troubleshooting in MATLAB Compiler SDK in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!