MATLAB Examples

Generate and Review Code with Mixed Languages and Mixed Locales

This example shows how to use the code generator to generate and review code for use in mixed languages and mixed locales.

Before using this example, see docid:rtw_ug.buzqe_q-2 or docid:ecoder_ug.brlpg7w-5.

The rtwdemo_unicode model configuration uses the Embedded Coder (R) ert.tlc system target file. To see internationalization and localization support with Simulink Coder®, configure the model to use the grt.tlc system target file. The example indicates the support that is specific to Embedded Coder® (for example, code generation templates).

The model configuration specifies files and settings that control how the code generator handles localization for:

  • C and C++ API interfaces
  • Code generation template (CGT) files (requires Embedded Coder®)
  • Target Language Compiler (TLC) files that apply code customizations (requires Embedded Coder®)

Contents

Open the example model rtwdemo_unicode.

Labels in the model appear in multiple languages (Arabic, Chinese, English, German, and Japanese) and various Unicode symbols.

model = 'rtwdemo_unicode';
open_system(model);

Verify Locale Settings

Verify that the locale setting for your MATLAB® software is compatible with your compiler. See the documentation for your operating system or the following MATLAB documentation:

Verify Model for Use of Foreign Characters

to verify the model for characters that the code generator cannot represent in the model's current character set encoding, use the Simulink® Model Advisor check Check model for foreign characters.

1. Open the Model Advisor in Simulink®. Select Analysis > Model Advisor > Model Advisor. Or, in the Command Window, type:

modeladvisor('rtwdemo_unicode')
Loading Model Advisor cache…
Warning: Cannot load an object of class
'SDRSLHDLWAPlugin':
Its class cannot be found. 
Warning: Cannot load an object of class
'SDRSLHDLWAPlugin':
Its class cannot be found. 
Model Advisor cache loaded. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.
Updating Model Advisor cache…
Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

2. Expand By Product.

3. Expand Simulink.

4. Select Check model for foreign characters

5. Click Run This Check.

6. Review the results. Several warnings appear. Verify that the characters in the model can be represented in the current character set encoding.

7. Close the Model Advisor.

Code Generation Template Files

To use a code generation template file with unicode characters when generating code, complete these steps (requires Embedded Coder®). Otherwise, go to the next section.

1. Open the Configuration Parameters dialog box.

2. Navigate to the Code Generation > Template pane. The model is configured to use the code generation template file rtwdemo_unicode.cgt. That file adds comments to the top of generated code files. For the code generator to apply escape sequence replacements for the .cgt file, enable replacements by specifying:

<encodingIn = "encoding-name">

3. Open the file /toolbox/rtw/rtwdemos/rtwdemo_unicode.cgt.

edit rtwdemo_unicode.cgt

4. Find the line of code that enables escape sequence replacements for the character set encoding UTF-8.

<encodingIn = "UTF-8">

5. Close the file /toolbox/rtw/rtwdemos/rtwdemo_unicode.cgt.

Generated File Customization Template

To use file customization templates with unicode characters when generating code, complete these steps (requires Embedded Coder®). Otherwise, go to the next section.

You can specify customizations to generated code files by using TLC code. TLC files support user default encoding only. To produce international custom generated code that is portable, use the 7-bit ASCII character set.

1. Open the Configuration Parameters dialog box.

2. Navigate to the Code Generation > Template pane. The model is configured to use the code customization file example_file_process.tlc. That file customizes the generated code just before the code generator writes the code files. For example, the file adds a C source file, corresponding include file, and #define and #include statements.

3. Open the file /toolbox/rtw/rtwdemos/example_file_process.tlc.

edit example_file_process.tlc

4. Before generating code, uncomment the following line of code:

%% %assign ERTCustomFileTest = TLC_TRUE%

5. Close the file /toolbox/rtw/rtwdemos/rtwdemo_unicode.cgt.

Generate C Code

Generate C code and a code generation report.

evalc('rtwbuild(''rtwdemo_unicode'')');

Review the Generated Code

For characters that are not in the current MATLAB® character set encoding, the code generator uses escape sequence replacements to render characters correctly in the code generation report.

1. If the code generation report for model rtwdemo_unicode is not open, in the Command Window, type:

coder.report.open('rtwdemo_unicode')

2. Review the generated code in rtwdemo_unicode.c and rtwdemo_unicode.h. Names of model elements appear in code comments as replacement names in the local language.

3. Open the Traceability Report. The report maintains traceability information, even when the name contains characters that are not represented in the current encoding. Names of model elements appear in the report as replacement names in the local language.

4. Scroll down to and click the code location link for the first Chart (State 'Selection' <S2>:23). The report view changes to show the corresponding code in rtwdemo_unicode.c.

5. In the code comment, click the <S2>:23 link. The model window shows the chart in a new tab.

6. In the model window, right-click that chart. Select C/C++ Code > Navigate to C/C++ Code. The report view changes to show the named constant section of code for that chart.

7. Close the code generation report, Model Advisor, and model. In the Command Window, type:

coder.report.close();
bdclose('all');

Generate C++ Code

Generate C++ code and a code generation report.

1. Open the model.

model = 'rtwdemo_unicode';
open_system(model);

2. Change Configuration Parameters > Code Generation > Language to C++. Or, in the Command Window, type:

set_param('rtwdemo_unicode','TargetLang','C++');

3. Change Configuration Parameters > Code Generation > Interface > Code interface packaging to C++ class. Or, in the Command Window, type:

set_param('rtwdemo_unicode','CodeInterfacePackaging','C++ class');

4. Generate C++ code and a code generation report.

evalc('rtwrebuild(''rtwdemo_unicode'')');

5. To see internationalization and localization support, review the generated code. See Review the Generated Code.

6. Close the code generation report and model. In the Command Window, type:

coder.report.close();
bdclose('all');