Efficiently call MATLAB code from another coding language

조회 수: 2 (최근 30일)
Elliot Bontoft
Elliot Bontoft 2021년 3월 29일
댓글: Elliot Bontoft 2021년 3월 29일
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.

채택된 답변

Walter Roberson
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
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.
Elliot Bontoft
Elliot Bontoft 2021년 3월 29일
Thank you for clarifying. I will investigate Automation Sever.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Troubleshooting in MATLAB Compiler SDK에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by