Is there a way to disable line wrapping in generated C code from MATLAB Coder?

7 views (last 30 days)
I am using MATLAB 2017b and MATLAB Coder to convert a MATLAB application into C. Due to the nature of the application, the generated code is quite long (thousands of lines), which is only amplified (and difficult to read) due to the automatic line wrapping that is occurring. E.g., hundreds of silly things like:
b_patches->data[points->size[0] + b_patches->size[0]] = (1.0 + (double)i)
- 1.0;
Is there any way to disable automatic line wrapping in the code generation? I've looked through the generation settings but did not see anything that seemed relevant. Thanks!

Accepted Answer

Ryan Livingston
Ryan Livingston on 30 Mar 2018
Edited: Ryan Livingston on 22 Mar 2019
Edit
As of MATLAB R2018b you can use the coder.EmbeddedCodeConfig setting ColumnLimit to control line wrapping in the generated code with MATLAB Coder and Embedded Coder. For older realeases read below.
Original Answer
There isn't a built-in way of doing this. I've made a note of this request so our team can consider adding such functionality in future releases.
For now a workaround could be to use an external code formatting tool. clang-format is quick to invoke and highly configurable. The Coder config setting PostCodeGenCommand allows you to run some MATLAB code after code generation finishes but before the C/C++ compiler runs. So you can use that to call clang-format.
  • Make the file doclangformat.m:
function doclangformat(buildInfo)
sourceFiles = join(buildInfo.getSourceFiles(true,true));
sourceFiles = sourceFiles{1};
cmd = ['clang-format -i -style=''{BasedOnStyle: LLVM, ColumnLimit: 20}'' ' sourceFiles];
system(cmd);
I've set ColumnLimit to 20 so the effect is obvious. The code will be drastically wrapped. You can view the other options in the clang-format documentation.
  • Set up the MATLAB Coder or Simulink Coder configuration and generate code
MATLAB Coder
cfg = coder.config('lib');
cfg.PostCodeGenCommand = 'doclangformat(buildInfo)';
codegen foo -config cfg <other codegen args>
Simulink Coder
Set the configuration parameter PostCodegenCommand to doclangformat(buildInfo)
Now you should see that your code is wrapped to about 20 columns.
The main downside of this approach is that the other Coder style settings like IndentStyle, IndentSize, etc. will need to be specified in your clang-format specification.

More Answers (0)

Categories

Find more on MATLAB Coder 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!