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 Select Source Files page.

Specify Source Files

  1. On the Select Source Files page, type or select the name of the entry-point function test_code_style.m.

  2. In the Project location field, change the project name to code_style.prj.

  3. Click Next to go to the Define Input Types step. The app analyzes the function for coding issues and code generation readiness. If the app identifies issues, it opens the Review Code Generation Readiness page where you can review and fix issues. In this example, because the app does not detect issues, it opens the Define Input Types page.

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:

  1. Click Let me enter input or global types directly.

  2. Click the field to the right of x.

  3. From the list of options, select int16. Then, select scalar.

  4. Click Next to go to the Check for Run-Time Issues step.

Note

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

Check for Run-Time Issues

The Check for Run-Time Issues step generates a MEX file from your entry-point functions, runs the MEX function, and reports issues. This step is optional. However, it is a best practice to perform this step. Using this step, you can detect and fix run-time errors that are harder to diagnose in the generated 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. To open the Check for Run-Time Issues dialog box, click the Check for Issues arrow .

  2. In the Check for Run-Time Issues dialog box, enter code that calls test_code_style with an example input. For this example, enter test_code_style(int16(4)).

  3. Click Check for Issues.

    The app generates a MEX function. It runs the MEX function with the example input. If the app detects issues during the MEX function generation or execution, it provides warning and error messages. Click these messages to navigate to the problematic code and fix the issue. In this example, the app does not detect issues.

  4. Click Next to go to the Generate Code step.

Configure Code Generation Parameters

  1. To open the Generate dialog box, click the Generate arrow .

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

  3. Click More settings and set these settings:

    • 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 All Settings tab, make sure that Enable code traceability is selected.

Generate C Code

Click Generate.

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 code generator provides a link to the report.

View the Generated Code

  1. To open the code generation report, click the View 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.

Finish the Workflow

Click Next to open the Finish Workflow page.

The Finish Workflow page indicates that code generation succeeded. It provides a project summary and links to the generated output.

Key Points to Remember

  • To check for run-time issues before code generation, perform the Check for Run-Time Issues step.

  • To access build configuration settings, on the Generate Code page, open the Generate dialog box, and then click More Settings.

Related Topics