# Automatically generate C code

Exploration of design ideas is the foundation of any design workflow. It helps create compelling products with unique value propositions that separate them from competition. For engineers, idea exploration takes the form of prototyping and testing algorithm variations to evaluate their behavioral and functional effect on the final product design. Such evaluation narrows the set of algorithm alternatives and helps identify the one that eventually make its way into the design of the entire system.

The speed with which an engineer can design and assess algorithm variations is critical to efficient optimization of the design. The high level interpreted language of MATLAB is a natural choice for engineers to express their ideas during design exploration, because it lets them focus on algorithm behavior instead of low-level programming details.

As product development evolves from algorithm exploration to system design, integration, and implementation on hardware, the design specifications are typically translated by hand into a language such as C or C++. The C programming language has broad popularity and is widely accepted as the language of choice for many development phases including system design, application software development, and embedded software implementation.

**Why translate MATLAB to C/C++?**

Often engineers translate MATLAB algorithms to C/C++ code to create standalone software prototypes, integrate MATLAB algorithms with other software written in C/C++, or deliver specifications to embedded software engineers for implementation. Generating C automatically (and nearly instantaneously) from MATLAB speeds design exploration and product development iterations.

Why is manual conversion of MATLAB to C difficult?

The pitfalls of manual translation stem from differences in the programming paradigms of MATLAB and C. MATLAB has a succinct syntax and native data types for the vector and matrix processing operations used in embedded signal processing and control algorithms. The equivalent C code requires multiple loops to process individual data samples, resulting in many lines of C code for a single line of MATLAB code. Equally important, the polymorphic and dynamically typed behavior that makes MATLAB flexible and easy to use is not readily accessible in the more statically typed C language without detailed programming. These differences make manual translation a time-consuming and complex process.

Manual translation also results in multiple copies of the same algorithm written in different languages. The engineer must verify that these copies remain equivalent throughout multiple design iterations. The cost of verifying and updating revisions to accommodate requirement changes quickly becomes prohibitive, often resulting in errors and a design that diverges from the original specification.

**Benefits of Automatic Translation **

Automating translation of MATLAB code to C code overcomes most, if not all, of the problems associated with manual translation. Automatic MATLAB-to-C code generation offers the following advantages:

- Design engineers spend more time innovating and tuning high-level algorithms in MATLAB rather than writing and debugging low-level C code
- The equivalence of MATLAB algorithms and generated C code is easily maintained during design iterations
- C code is produced much faster and is free of the errors that are frequently introduced by hand coding

The automatic translation workflow from MATLAB allows engineers to maintain “one truth”— and elaborate it directly within MATLAB to incorporate implementation details such as specifying data types and sizes of input and output variables. Design iterations become easier because engineers continue to use the interactive debugging and visualization capabilities in MATLAB throughout the process. The significant cost of producing and verifying hand-written C code can be eliminated for common design workflow uses such as prototyping algorithms as standalone executables, integrating algorithms as reusable libraries, and accelerating parts of the algorithm’s execution in MATLAB.