Main Content

Controlling C Code Style

About This Tutorial

Learning Objectives

This tutorial shows you how to:

  • Generate code for if-elseif-else decision logic as switch-case statements.

  • Generate C code from your MATLAB® code using the MATLAB Coder™ app.

  • Configure code generation configuration parameters in the MATLAB Coder project.

  • Generate a code generation report that you can use to trace between the original MATLAB code and the generated C code.

Required Products

This tutorial requires the following products:

Required Files

TypeNameDescription
Function codetest_code_style.mMATLAB example that uses if-elseif-else.

Create File in a Local Working Folder

  1. Create a local working folder, for example, c:\ecoder\work.

  2. Create a file test_code_style.m that contains this code:

    function y = test_code_style(x) 
    %#codegen
    
    if (x == 1)
        y = 1;
    elseif (x == 2)
        y = 2;
    elseif (x == 3)    
        y = 3;
    else
        y = 4;
    end

Open the MATLAB Coder App

On the MATLAB Toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon. The app opens the Create MATLAB Coder Project dialog box.

Provide the name of the project file and the folder in which you want to place the file. For this example, create a file named code_style.coderprj in your current working folder.

Specify Source Files

  1. Either click the Add Entry Points button in the Inputs section of the MATLAB Coder panel or the Entry Points button in the toolstrip.

  2. The Entry Points tab opens. Enter the name of your entry-point function test_code_style.

  3. The app runs the Code Generation Readiness Tool on the entry-point function. This tool screens the MATLAB code for features and functions that are not supported for code generation. If the app identifies issues with an entry-point function or one of its dependencies, it displays a warning message in the Entry Points tab. In this example, the app does not detect code generation readiness issues in the test_code_style function.

Define Input Types

Because C uses static typing, at compile time, the code generator must determine the properties of all variables in the MATLAB files. Therefore, you must specify the properties of all function inputs. To define the properties of the input x, in the Entry Points tab:

  1. Expand the test_code_style node.

  2. Click the field to the right of x. From the list of options, select int16. Leave the size specification to its default value of 1-by-1.

Generate and Run MEX Function

A MEX function is generated code that can be called from inside MATLAB. Before you generate standalone C/C++ code, it is a best practice to generate and run a MEX function. This step enables you can detect and fix run-time errors that are harder to diagnose in the generated standalone C/C++ code. By default, the MEX function includes memory integrity checks. These checks perform array bounds and dimension checking. The checks detect violations of memory integrity in code generated for MATLAB functions. For more information, see Control Run-Time Checks.

  1. In the Prepare section of the MATLAB Coder tab of the toolstrip, set Output to C and Build type to MEX. Use the default values for the other code configuration settings.

  2. In the Generate section of the toolstrip, open the Generate Code drop-down menu and select the Generate Code and Build option.

The Output section of the code generation panel indicates that code generation succeeded.

The generated MEX function test_code_style_mex is located in your current working folder. In the Command Window, run the MEX function with example inputs. For example:

test_code_style_mex(int16(4));
The generated MEX runs without errors.

Configure Code Generation Parameters

In the Prepare section of the MATLAB Coder tab of the toolstrip:

  1. Set the Build type to Static Library (.lib).

  2. Click Settings and set these parameters:

    • On the Code Appearance tab, select the Convert if-elseif-else patterns to switch-case statements check box.

    • On the Debugging tab, make sure that Always create a report is selected.

    • On the Advanced tab, make sure that Enable code traceability is selected.

Note

The Convert if-elseif-else patterns to switch-case statements optimization works only for integer and enumerated type inputs.

Generate Standalone C Code

In the Generate section of the toolstrip, open the Generate Code drop-down menu and select the Generate Code and Build option.

When code generation is complete, the code generator produces a C static library, test_code_style.lib, and C code in the /codegen/lib/test_code_style subfolder. The Output section of the MATLAB Coder panel provides a link to the report.

View the Generated Code

  1. To open the code generation report, click the Code generation report link.

    The test_code_style function is displayed in the code pane.

  2. To view the MATLAB code and the C code next to each other, click Trace Code.

  3. In the MATLAB code, place your cursor over the statement if (x == 1).

    The report traces if (x == 1) to a switch statement.

See Also

Topics