Global Sources
EE Times-India
 
EE Times-India > EDA/IP
 
 
EDA/IP  

Translating MATLAB-to-C

Posted: 19 Sep 2008     Print Version  Bookmark and Share

Keywords:MATLAB  C code  translation  signal processing 

This first part of the series examines the basic problems of translating basic MATLAB functions to C. Part 2 will discuss complex functions and the role C interface constraints play in the translation process. Part 3 will examine the verification process and makes the case for automatic C generation.

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 (see Table 1 for examples).

image name

Table 1: Examples of goals and requirements of translated C code.

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

1 • 2 Next Page Last Page



Comment on "Translating MATLAB-to-C"
Comments:  
*  You can enter [0] more charecters.
*Verify code:
 
 
Webinars

Seminars

Visit Asia Webinars to learn about the latest in technology and get practical design tips.

 

Go to top             Connect on Facebook      Follow us on Twitter      Follow us on Orkut

 
Back to Top