During the simulation of your test file, the MATLAB® Coder™ app detects unexecuted code or code that is constant folded. Code that is not executed by the test bench may be unreachable code or dead code. The app uses the code coverage information when translating your code from floating-point MATLAB code to fixed-point MATLAB code. Reviewing code coverage results helps you to verify that your test file is exercising the algorithm adequately.
The app inserts inline comments in the fixed-point code to mark the unexecuted and untranslated regions. It includes the code coverage information in the generated fixed-point conversion HTML report. The app editor displays a color-coded bar to the left of the code. This table describes the color coding.
|Coverage Bar Color||Indicates|
One of the following situations:
Different shades of green indicate different ranges of line execution counts. The darkest shade of green indicates the highest range.
|Orange||The entry-point function executes multiple times, but the code executes one time.|
|Red||Code does not execute.|
Unexecuted code is code that is not executed by the test bench during simulation. Unexecuted code can result from these scenarios:
Defensive code containing intended corner cases that are not reached
Human error in the code, resulting in code that cannot be reached by any execution path, sometimes referred to as unreachable code or dead code
Inadequate test bench range which does not provide inputs that execute all paths in the code
This example shows how to detect code in your algorithm that is not executed by the test bench by using the MATLAB Coder app.
In a local writable folder, create the function
function y = myFunction(u,v) %#codegen for i = 1:length(u) if u(i) > v(i) y=bar(u,v); else tmp = u; v = tmp; y = baz(u,v); end end end function y = bar(u,v) y = u+v; end function y = baz(u,v) y = u-v; end
In the same folder, create a test file,
u = 1:100; v = 101:200; myFunction(u,v);
From the apps gallery, open the MATLAB Coder app.
Set Numeric Conversion to
Convert to fixed
On the Select Source Files page, browse to the
myFunction file, and click
Click Next. On the Define Input
Types page, browse to select the test file that you created,
myFunction_tb. Click Autodefine Input
Click Next. On the Check for Run-Time Issues page, click Check for Issues.
The app runs the
myFunction_tb test file and detects no
Click Next. On the Convert to Fixed-Point page, click Analyze to simulate the entry-point functions, gather range information, and get proposed data types.
The color-coded bar on the left side of the edit window indicates whether the
code executes. The code in the first condition of the if-statement does not
execute during simulation because u is never greater than
bar function never executes
because the if-statement never executes. These parts of the algorithm are marked
with a red bar, indicating that they are not executed by the test bench.
To apply the proposed data types to the function, click Convert .
Coder app generates a fixed-point function,
myFunction_fixpt. The generated fixed-point code contains
comments around the pieces of code identified as not being executed by the test
bench. The Validation Results pane proposes that you use a
more thorough test bench.
When the MATLAB Coder app detects unexecuted code, consider editing your test file so that your algorithm is exercised over its full range. If your test file already reflects the full range of the input variables, consider editing your algorithm to eliminate the unreachable code.
Close the MATLAB Coder app.
Edit the test file
myFunction_tb.m to include a wider range
u = 1:100; v = -50:2:149; myFunction(u,v);
Reopen the MATLAB Coder app.
Using the same function and the edited test file, go through the conversion process again.
After you click Analyze, this time the code coverage bar shows that all parts of the algorithm execute with the new test file input ranges.
To finish the conversion process and convert the function to fixed point, click Convert.