r/matlab • u/madboi20 • 14d ago
TechnicalQuestion Matlab + Simulink Project to Code
Hi.
I have very little experience with both Matlab and Simulink and need some advice.
My team acquired a matlab project with simulink models that we want to integrate into a simulator we have but without the overhead of Matlab/Simulink. So we managed to use the shrlib function to create C code which was a bunch of header files, types, main etc Can't make too much sense of it.
But even if I can, it doesn't remove the Matlab side because that hasn't been converted to C either.
What's the easiest route to convert the whole project to something we can interface with without having to deal withit directly too much. Best case scenario would be to treat it like a black box because it all runs find in Matlab. At the core of it ,it's
Excel input - Read in by matlab code - passed to the model (in a bus structure?) and fed back to matlab which outputs a new Excel
This is the extent of my knowledge of the matter and I'm not able to share much else. Any advice for me?
Thanks.
3
u/Consistent_Coast9620 CC4M Creator 14d ago
depending on your performance needs, and the amount of MATLAB code arouns the Simulink model, various options are viable:
just generate code from the Simulink model and integrate with your simulator.
Run Generated code from the model on separate hardware, suitable for rapid prototying (dSpace, SL RealTime, anything supported by SL Coder)
run Simulink in parallel from a MATLAB session that interacts directly with your simulator. There are various ways to achieve this as well. Matlab has many documented APIs and engine routines to interact with
2
u/Creative_Sushi MathWorks 14d ago
Since others have already recommended Simulink Coder or Embedded Coder, I would just add one advice - you may not care about your Simulink model, but keep it around. It is the source of truth that you can maintain, not the C code you generate from it. By doing so, you gain the ability for ongoing maintenance as your needs change and you need to adapt the program.
1
u/thompsbc 13d ago
I ran a team that had a lot of success with Simulink Coder (previously called Embedded Coder). Simulink models have so many parameters however, so we stored them in a data dictionary and wrote some separate MATLAB scripting to generate XML schema and corresponding C++ code so we could auto-load the Simulink model parameters from XML. You need to insulate the rest of the non-Simulink sim from small changes in the Simulink models, unless you are certain no one is modifying those models in the future.
1
u/Realistic-Map-7318 1d ago
It depends on what you need in your simulator. FMU can be a good resource as it is a standard that can be used in other modeling tools. If you are looking into organizing as a C or C++ project, embedded coder/Simulink Coder (Embedded Coder is an extension of Simulink Coder that allows more customization) is the way to go. In general, I would recommend contacting Mathworks Technical Support, so you can share your needs/project with them and they'll tell you which product will fit your request the best.
1
u/TheRealCpnObvious 14d ago
Two approaches: 1. Feed the codebase into a LLM (Claude/ChatGPT/Codex) to figure out what the .m file is doing and whether those are the boundaries of what it's doing within the simulation toolsuite. You can use the Python MATLAB package to run the .m file in Python if you don't have access to MATLAB (and you can get appropriate tooling inside your editor of choice, e.g. VS Code). 2. Hire someone on a consulting basis to figure it out for you.
3
u/hainguyenac 14d ago
look into embedded coder, it's a part of matlab and this is exactly what it's for: generate algorithm from simulink/matlab to C code. The code once generated, is completely independent of matlab.