How to convince Matlab Coder to emit one C function per Matlab function

3 views (last 30 days)
Hi, I'm working with Matlab Coder to generate C code for some Matlab files we have. So far I haven't run into any serious problems aside from some inconveniences.
Today I constructed a short function, let's say it's named FooCaller, which assigns some variables and then calls a much longer, important function named Foo. I used Matlab Coder to generate code for FooCaller and it did so dutifully, and the code works as expected, so that's great.
However, on inspecting the generated code, I see that Matlab Coder constructed a C function for FooCaller, but then pasted the code for Foo into the FooCaller function, instead of translating Foo separately and then calling the C function for Foo from the C function for FooCaller.
This is a bit of a bummer because FooCaller itself is not very important, and it will probably get replaced with some hand-written C code. Is there a way to tell Matlab Coder to construct a separate C function for each Matlab function which is encountered?
I guess I could specify all of the Matlab functions as entry points for translation, and then a separate C function would be generated for each one, right? But there are a lot of functions, so that's error prone; it would be easy to overlook one.
Thanks in advance for any light you can shed on this question.

Accepted Answer

Denis Gurchenkov
Denis Gurchenkov on 25 Jul 2018
The best way would be to generate code for Foo directly, skipping FooCaller. That is, in the MATLAB Coder App, pick Foo.m as the entry point. If you are using command line tool (codegen) then do "codegen Foo ....args..." instead of "codegen FooCaller ...args ...". This way, you have complete control over the prototype of Foo, you can specify exactly what input types should it have in the C code and you can observe the output types.
If for some reason you do want to use FooCaller as an entry point, you can tell MATLAB Coder that Foo should remain separate and not be inlined into FooCaller. You can do that by adding
in the body of Foo.
This will keep Foo and FooCaller as separate functions and will generate Foo.c and FooCaller.c (unless you configured MATLAB Coder to bundle all functions into one .c file). However, this way you don't have complete control over the prototype of Foo -- coder may decide to optimize it by hard-coding certain inputs, or removing outputs that are not used inside FooCaller etc.
Does this make sense?

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by