As you in all probability know, it’s doable to combine C/C++ code in Simulink simulations. This may be achieved in some ways, together with:
- C/C++ S-Perform
- S-Perform Builder
- Legacy Code Device
- C Caller
- C Perform
- MATLAB Perform
- MATLAB System
- Stateflow chart
In current releases, now we have drastically simplified the method. On this put up, I’ll spotlight how this may be achieved in two methods:
Preliminary Setup
This web page lists the supported blocks together with the compilers and IDEs supported for various working techniques.
On Home windows, to make use of the Microsoft Visible Studio debugger, you’ll seemingly must set a Home windows registry key. You’ll be able to see if this registry key entry is already configured by executing this line of code:
winqueryreg(‘HKEY_LOCAL_MACHINE’,‘SYSTEMCurrentControlSetControlSession Supervisor’,‘DebuggerMaxModuleMsgs’)
Set Breakpoints in Customized Code
As an instance this course of, I used the next easy C file and header:

I created a brand new mannequin and specified the C file in its configuration:

I then switched to the Embody headers part and used the Auto-fill from supply information button to routinely fill the embrace headers textual content:


I linked inputs and outputs to the C Caller block to have a working simulation:


Within the Debug tab of the Simulink mannequin, I can then click on on Set Breakpoints in Customized Code:

The mannequin standing bar will inform us that it’s launching the exterior debugger

Microsoft Visible Studio will open the C file and routinely connect to the MATLAB course of. At this level, you’ll be able to set breakpoints within the customized code, click on play in Simulink, and examine the C code when the breakpoints is hit:

MinGW and Visible Studio Code
system(“code –install-extension ” + …
fullfile(matlabshared.supportpkg.getSupportPackageRoot(),…
“toolbox”, “targetframework”, “supportpackage”,…
“vscodedebug”, “extension”,…
“matlab-coder-interface-vscode-debugging.vsix”))

As soon as that is achieved, the method is just like the one described above with Visible Studio. Click on the Set Breakpoints in Customized Code button within the Debug tab of the toolstrip, this may launch Visible Studio Code and fix it to the MATLAB course of. Add the breakpoints within the code and simulate the mannequin.

Now it is your Flip
Are you leveraging this function to set breakpoints and debug your customized C code? Do you like to make use of Visible Studio or Visible Studio Code to debug your C code?