Design Article

MATLAB-to-C translation, part 1: Pitfalls and problems

Marc Barberis and Luc Semeria, Catalytic

11/29/2007 3:00 AM EST

Part 2 looks at complex functions and the role C interface constraints play in the translation process. It will be published Thursday, December 6. For a related product story, see MATLAB-to-C tool get major upgrade.

Most signal processing and communication projects nowadays at some point require translating MATLAB code into equivalent C code. Goals and requirements of the resulting C code can be diverse. Examples include:


While C code requirements vary widely, some translation pitfalls are common across all applications and teams. Many of these pitfalls derive from the fact that MATLAB is essentially an interpreted language. Consequently, it does not require a priori knowledge. I.e., MATLAB doesn't know the type, shape, dimension or even the existence of any variable or function until execution.

Let us list some of the most common or bothersome pitfalls:

  • MATLAB indexes array elements starting with 1 whereas C indexing starts with 0
  • MATLAB is column major whereas C is row major. This means consecutive data in MATLAB are column elements whereas consecutive data in C are row elements.
  • MATLAB is inherently a vector-based representation. This can make the translation challenging. In particular:
    • You must replace the simplest vector operations with a loop construct
    • Operators (such as times '*') in MATLAB perform different operations depending on the type of the operands
    • MATLAB includes very simple and powerful vector operations such as the concatenation "[]"and column "x(:)" operators or "end" construct, which can be quite hard to map to C

  • MATLAB supports "polymorphism" whereas C does not. I.e., you can write a generic function in MATLAB, which can process different types of input parameters. In C, each parameter has one given type, which cannot change.
  • MATLAB supports dynamic extensions and sizing of arrays, whereas C code requires storage to be allocated explicitly using malloc/free.
  • MATLAB draws on a rich set of libraries that are not available in C. Implementing such functions requires writing new code. Sometimes there are pre-exiting libraries and functions available for your target platform, but integrating them into your application can be problematic. In addition to running into licensing issues (such using GPL code for commercial application), interfacing with these libraries is non-trivial.
  • MATLAB supports reusing the same variable for different contents (different types). C does not, as each variable has one unique type.

As we will see, the process of writing C code from MATLAB is trickier than it sounds.

Preliminary step - What are my inputs?
When converting MATLAB to C, the first thing you need is a description of your function's input parameters. This is because MATLAB, being an interpreted language, does not require declaration of data type for any variable.

Complicating matters, MATLAB variables have the following features not available in C

  • The ability to have a variable number of parameters

  • MATLAB data types that have no C equivalent, such as "cell arrays", a collection of heterogeneous types packed into slices of an array
Typically, information about input parameters can be found in comments embedded at the top of the MATLAB function, or in a report written by the MATLAB code implementer.

Conversion Examples
With the input parameters defined, we can now turn to the MATLAB code itself. To illustrate some basic issues with translating MATLAB to C, we will show three examples, which we will translate by hand.

Example 1 – Simple(?) MATLAB code
Let's start with a simple example. The MATLAB code below take in a vector 'x', and returns all but the first two values, sorted in ascending order, that are greater than a given threshold.

In MATLAB, this is an easy feat:


We can run it on a simple case:


Even such a trivial example, which does not make use of any advanced matrix or mathematical features of MATLAB, presents a number of problems for the C implementer:


An example implementation, as written in syntactically correct C, is shown below (where the "my_sort" function still has to be implemented):


We have only scratched the surface, but clearly, there is nothing obvious about translating such simple MATLAB code into C.





amigan50

12/4/2007 9:20 AM EST

why i+=1 instead of simply i++?

"malloc" without a corresponding "free"
and vice versa is a bad idea!
you free sorted which was not malloc-ed,
and you malloc y_out without freeing it.

Sign in to Reply



luc_semeria

12/4/2007 1:19 PM EST

Writing C code by hand is very error prone! Your comment is right on. The fact that such a simple example may lead to errors or differences of opinion speaks volume for the need for an automatic translation tool such as Catalytic MCS.

The example above assumes memory gets allocated inside of functions. Function callers are then responsible for freeing that memory.

The example works like this. The malloc corresponding to "free(sorted)" happens in "my_sort". The free corresponding to "y_out = malloc(...);" happens in the caller of "example1".

Finally, "i+=1", "i++" and "i=i+1" are all valid loop increments that should lead to the same assembly code out of today's C compilers.

Sign in to Reply



Please sign in to post comment

Navigate to related information

Datasheets.com Parts Search

185 million searchable parts
(please enter a part number or hit search to begin)