Use Matlab Engine within DLL

5 vues (au cours des 30 derniers jours)
Martin Werner
Martin Werner le 23 Août 2021
Réponse apportée : Pratyush le 16 Avr 2024 à 5:53
Dear Ladies and Gentlemen,
I want to use Matlab together with my circuit simulator. I was able to write a standalone executable that launches the matlab engine and does some calculations. Now I would like to do the same using a DLL. It must be an DLL since the simulator can only load DLLs and I need to start the Matlab Engine within the DLL. I am using Visual Studio 2019 for compilation.
When I try to do this, the simulator crashes as soon as I call "matlabPtr = startMATLAB();". So I was wondering if it is even possible to call Matlab from an DLL and if so, what things I have to be aware of.
Thank you very much!
Best regards,
Martin

Réponses (1)

Pratyush
Pratyush le 16 Avr 2024 à 5:53
Hi Martin,
Integrating MATLAB with a DLL for use in applications like circuit simulators involves several key considerations:
  1. Use MATLAB Engine API for C++ correctly to interact with MATLAB from C++.
  2. Ensure Thread Safety, as the MATLAB Engine API isn't inherently thread-safe.
  3. Properly Initialize and Shutdown the MATLAB engine to avoid resource leaks or crashes.
  4. Manage Memory carefully when transferring data between C++ and MATLAB to prevent leaks or corruption.
  5. Correct Compilation and Linking against MATLAB Engine API headers and libraries is crucial, ensuring compatibility between Debug/Release configurations and MATLAB versions.
  6. MATLAB Runtime might be required for machines without MATLAB installed, respecting MATLAB's licensing requirements.
  7. Troubleshoot with steps like checking for exceptions, dependency issues, MATLAB version compatibility, using debugging tools, and consulting MATLAB logs.
The process requires careful handling of the MATLAB Engine API, attention to thread safety, memory management, and proper setup of your development environment.

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by