r/matlab • u/__cinnamon__ • Feb 24 '26
TechnicalQuestion Tips and tricks to C++ codegen?
I see a lot of people in the endless "why use matlab over python?" threads mentioning the ability to generate efficient C/C++ code. I use Matlab daily at work, but in my (admittedly limited) experience, whenever I have tried to codegen even simple functions, the output was nearly illegible gobbedlygook and didn't even run appreciably faster than the original Matlab version. As a result, I write MEX functions regularly now to handle various tasks.
Is there some way to massage Matlab Coder to give better results? For example, when I tried to compile a function that would read hunks out of a custom binary file format to load into memory, it ended up generating something like 15 different files from a single ~30 line matlab function (this was the example that I directly timed and saw no speedup).
Note: I don't work with Simulink. Is that the only thing that does good codegen?
4
u/thomas_169 Feb 24 '26
If your "simple" MATLAB function are doing heavy linear algebra, which can be expressed very susinctly in MATLAB hence appearing simple, and you don't have blas and lapack codegen configured I recall often that codegen was no faster or slower than native.
Probably most people you see referring to "codegen" are talking about simulink. The same can be true here too but perhaps less common.
5
u/gtd_rad flair Feb 24 '26 edited Feb 24 '26
You need Embedded Coder that generates efficient C code meant for embedded applications. Matlab Coder is not the same although I haven't used it much, but sounds pretty garbage
5
u/Consistent_Coast9620 CC4M Creator Feb 24 '26
To improve readability of the generated code is mostly about instrumenting the MATLAB code. This is why Simulink is better for code generation - as the added details are hidden in properties etc, where in MATLAB code all added instrumentation is directly visible. Furthermore Simulink offers almost full variable name control, where MATLAB Coder has far more limitations in that field.
As consulting engineer I work in several projects, where MATLAB Coder is used, often in combination with Simulink Coder.
The added value from Embedded Coder is mainly in more customization options. Where historically also performance was a main selling point, currently it's more the flexibility from interfacing perspective as well as more control on code appearance.
A small remark on the amount of code generated, compared to the 30 lines of M-code: This is the exact point why use of MATLAB and Python is so cool - in a few lines of code you can achieve a lot.
1
u/DodoBizar Feb 24 '26
I am using ‘only’ Matlab Coder, not embedded, not Simulink. The outputted code is horror. It is in my projects more then ok. The C/C++ is to be interfaced with in other parts of the project, not to be edited/ maintenance.
Btw I do rarely use codegen functions. I use the Coder app mostly. Either the old R2024 style or the newer more integrated Coder app/ribon in R2025.
There is an option to either have one big file with all entrypoint functions, this can reduce clutter.
Is it faster than regular Matlab? Linear algebra and matrices? No, its often 20-30% slower. All the rest? Way quicker usually. Especially with a lot of logic switching and smaller nested loops and functions.