MATLAB Answers

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

12 ビュー (過去 30 日間)
Thomas Watson
Thomas Watson 2018 年 3 月 29 日
編集済み: Ryan Livingston 2019 年 3 月 22 日
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 件のコメント

サインインしてコメントする。

採用された回答

Ryan Livingston
Ryan Livingston 2018 年 3 月 30 日
編集済み: Ryan Livingston 2019 年 3 月 22 日
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 件のコメント

Thomas Watson
Thomas Watson 2018 年 3 月 30 日
Thanks for the info and the consideration, Ryan.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by