The most efficient method to start a matlab code within a C-Routine
1 view (last 30 days)
Show older comments
As a part of my masters thesis I am programming an external C-Subroutine for a MBS-Software in order to work as a force element like e.g.:
force=subfunction(displacement)
There is a Matlab code doing this and very time-consuming to implement it in C.
So the following options I considered to overcome this the most efficient way:
1. Using Matlab-Compiler to create a .exe and start it from C
Very nice and easy solution. However the problem is that the mbs-solver calls the function >10000 times and the compiled Matlab executable has to load the MCR (Matlab Compiler Runtime) every single time! The processing time is very short (~0.001 seconds) but the loading of MCR takes about 5 seconds (!) for each iteration step. This makes this option unfortunately useless for me.
"Every time you start a compiled executable, you essentially start another instance of MATLAB."
2. Using Matlab-Compiler to create a shared library etc.
I haven't tried this. As far as I researched I understood that also for these options the MCR has to be started every single step like the option 1.
3. Using the Matlab-Coder to generate a C-Code
It's not an easy option as far as I tried sometimes. The generated code should be optimized. Would it really be more efficient?
4. Starting Matlab-Executable only the first time without closing it and communicating the C-Code over any files
Just a conception: I would like to start Matlab-Exe. in the first iteration step and let write a file which contents "displacement". Then I would scan every milisecond the file from Matlab if any new value came. I would process it and write in another file and the C-Routine that scans the result file every milisecond would find the new value and keep working.
I can imagine that this works faster but surely it's bit tricky to implement until it works bugfree.
I am thankful for any reports about your experiences or ideas.
0 Comments
Accepted Answer
Ryan Livingston
on 13 Sep 2013
Hopefully someone corrects me if I am wrong but option 2 should give you what you want if you are using MATLAB Compiler already. The general workflow for generating and using a shared library is described here:
Notice that the MCR need only be initialized once. Then your entry point function(s) can be called at will and should utilize that same MCR.
Option 3 will be able to give you fully standalone C code which runs free from the MCR. If you need such a thing then that may be desirable.
0 Comments
More Answers (0)
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!