Main Content

Detect and Debug Code Generation Errors

Debugging Strategies

To prepare your algorithms for code generation, MathWorks recommends that you choose a debugging strategy for detecting and correcting violations in your MATLAB® applications, especially if they consist of a large number of MATLAB files that call each other's functions. Here are two best practices:

Debugging StrategyWhat to DoProsCons

Bottom-up verification

  1. Verify that your lowest-level (leaf) functions are suitable for code generation.

  2. Work your way up the function hierarchy incrementally to compile and verify each function, ending with the top-level function.

  • Efficient

  • Safe

  • Easy to isolate syntax violations

Requires application tests that work from the bottom up

Top-down verification

  1. Declare all functions called by the top-level function to be extrinsic so fiaccel does not compile them.

  2. Verify that your top-level function is suitable for code generation.

  3. Work downward in the function hierarchy to:

    a. Remove extrinsic declarations one by one

    b. Compile and verify each function, ending with the leaf functions.

Lets you retain your top-level tests

Introduces extraneous code that you must remove after code verification, including:

  • Extrinsic declarations

  • Additional assignment statements as necessary to convert opaque values returned by extrinsic functions to nonopaque values.

Error Detection at Design Time

To detect potential issues for MEX file building as you write your MATLAB algorithm, add the %#codegen directive to the code that you want fiaccel to compile. Adding this directive indicates that you intend to generate code from the algorithm and turns on detailed diagnostics during MATLAB code analysis.

Error Detection at Compile Time

Before you can successfully generate code from a MATLAB algorithm, you must verify that the algorithm does not contain syntax and semantics violations that would cause compile-time errors, as described in Detect and Debug Code Generation Errors.

fiaccel checks for all potential syntax violations at compile time. When fiaccel detects errors or warnings, it automatically produces a code generation report that describes the issues and provides links to the offending code. See Code Generation Reports.

If your MATLAB code calls functions on the MATLAB path, fiaccel attempts to compile these functions unless you declare them to be extrinsic.