MATLAB Answers

1

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

Asked by Thomas Watson on 29 Mar 2018
Latest activity Edited by Ryan Livingston on 22 Mar 2019
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!

  0 Comments

Sign in to comment.

1 Answer

Answer by Ryan Livingston on 30 Mar 2018
Edited by Ryan Livingston on 22 Mar 2019
 Accepted Answer

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.

  1 Comment

Thanks for the info and the consideration, Ryan.

Sign in to comment.