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.
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.