フィルターのクリア

Best/fastest way to call MATLAB routine from C repeatedly

6 ビュー (過去 30 日間)
David Aronstein
David Aronstein 2018 年 12 月 7 日
コメント済み: David Aronstein 2018 年 12 月 13 日
We have a MATLAB routine/script that we wish to call from a C program.
What we have tried to do so far is to use the MATLAB compiler to create a DLL, and then in the C program, using the Windows SDK, we load the DLL (LoadLibrary), and then we run Init, run the DLL, and run Close (all through calls using GetProcAddress).
We have successfully done this with a simple "hello_world.m" function that doesn't do anything interesting. What we have found is that the process above is really slow -- ~15 seconds to load the DLL and ~10 seconds to run it.
Our desired "use case" is to use MATLAB to implement a routine that would get called numerous times by the C program, and so this slowness is a real concern.
My questions are:
  • Is this slowness similar to what others have experienced?
  • Is there a smarter way to go about this whole goal, maybe in how we compile the MATLAB code, or how we interact with it from C, that gives better performance?
We've looked for examples of what we're trying to do, and there aren't a lot of them and none go into this sort of level of detail. It is much more common to run compiled C code from MATLAB, and maybe there are good reasons why people don't usually do it the other way around?

採用された回答

Koundinya
Koundinya 2018 年 12 月 10 日
You could try using the MATLAB Engine API tp speed up things a bit. The MATLAB Engine API for C and C++ provides a convenient way to evaluate MATLAB expressions or routines from your C program. Using the engine api your application could also connect to an existing session of MATLAB, to run your matlab routine/script.This approach should be faster because it doesn't have the overhead of loading the DLL.
  1 件のコメント
David Aronstein
David Aronstein 2018 年 12 月 13 日
This is very helpful! The fact that the MATLAB Engine API needs a full MATLAB installation is not ideal for our dumb logistical rasons, but this is definitely worth exploring in any case. Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2018 年 12 月 10 日
You must not do Loading, Init, Close repeatly.
They must be called once, then you must call whatever function you want repeatly with in Init and Close.
  1 件のコメント
David Aronstein
David Aronstein 2018 年 12 月 13 日
This was very helpful, thanks!
It inspired us also to look into how much of the run time was tied up into opening figure windows... and if we don't close the figure windows at the end of the DLL call, the runtime for the next time we call the DLL is fast enough (< 1-2 sec).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeC Shared Library Integration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by