Embedded MATLAB Coding Style

Writing Reusable Code

With the Embedded MATLAB subset, you can write reusable code in the following ways:

See How the Embedded MATLAB Subset Resolves Function Calls.

Common applications include:

Writing Efficient Code

The Embedded MATLAB subset provides methodologies for writing efficient code:

Unrolling for-Loops

Unrolling for-loops eliminates the loop logic by creating a separate copy of the loop body in the generated code for each iteration. Within each iteration, the loop index variable becomes a constant. By unrolling short loops with known bounds at compile time, Embedded MATLAB functions generate highly optimized code with no branches.

You can also force loop unrolling for individual functions by wrapping the loop header in an eml.unroll function. For more information, see eml.unroll in the Embedded MATLAB Function Reference.

Inlining Functions

The Embedded MATLAB subset uses internal heuristics to determine whether or not to inline functions in the generated code. In most cases, these heuristics produce highly optimized code. However, you can use the eml.inline directive to fine-tune these optimizations for individual functions. See eml.inline in the Embedded MATLAB Function Reference.

Working with Variables

Rules for Declaring Variables

Observe the following rules when defining variables in an Embedded MATLAB function:

Declare Variables Explicitly Before Using Them.   You must explicitly and unambiguously declare the size, type, and complexity of variables before using them in operations or returning them as outputs in Embedded MATLAB functions. To declare variables, assign them to data with known properties, as described in Declaring Variables By Assignment.

By default, the assignment copies the size, type, complexity, and value to the new variable. Initializing the new variable to the value of the assigned data sometimes results in redundant copies in the generated code. To avoid redundant copies, you can define variables without initializing their values by using the eml.nullcopy construct as described in Declaring Uninitialized Variables.

Do not change properties of variables after the initial assignment.   You cannot change size, type, or complexity of a variable after setting these properties as part of the initial assignment. In the following example, the last two statements each cause errors:

x = 2.75; % OK
y = [1 2; 3 4]; % OK
x = int16(x); % ERROR: cannot recast x
y = [1 2 3; 4 5 6]; %ERROR: cannot resize y

Declaring Variables By Assignment

As in MATLAB, you declare variables by assignment. However, in Embedded MATLAB code, you cannot change the size, type, or complexity of the variable after the initial assignment. Therefore, you must set these properties as part of the assignment declaration. The data that you assign to a variable can be a scalar, matrix, or structure.

For example, the following assignments define variables in an Embedded MATLAB function:

...
% a is a scalar of type double.
a = 14.7;

% b has properties of a, scalar of type double.
b = a;

% c is a 5-by-2 double array of zeros.
c = zeros(5,2);

% d has properties of c (5-by-2 double array of zeros).
d = c;

% e is 5-by-2 array of type double.
e = [1 2 3 4 5; 6 7 8 9 0];
...

When you declare variables, they are local by default; they do not persist between function calls. To make variables persistent, see Declaring Persistent Variables.

Using Type Cast Operators in Variable Declarations.   By default, constants are of type double. To declare variables of other types, you can use type cast operators in variable declarations. For example, the following code declares variables y and z as integers:

x = 15; % x is of type double by default.
y = int16(3); % y is a constant of type int16.
z = uint8(x); % z has the value of x, but cast to uint8.

Setting Properties of Indexed Variables.   Unlike in MATLAB, you cannot set the size of an indexed variable in an assignment statement. You must define the matrix first before assigning values to any of its elements.

For example, the following initial assignment is not allowed in Embedded MATLAB functions:

g(3,2) = 14.6; % Not allowed for creating g 
				     % OK for assigning value once created

For more information about indexing matrices, see Using Matrix Indexing Operations.

Declaring Uninitialized Variables

Variable declarations in the Embedded MATLAB subset not only copy the properties of the assigned data to the new variable, but also initialize the new variable to the assigned value. This forced initialization sometimes results in redundant copies in C code generated using Real-Time Workshop software. Embedded MATLAB technology eliminates many copies automatically. To eliminate copies that are not automatically handled, you can declare uninitialized variables by using the eml.nullcopy function.

Use caution when declaring uninitialized variables, as described in Rules for Declaring Uninitialized Variables.

Rules for Declaring Uninitialized Variables.   Follow these rules when declaring uninitialized variables:

Example: Declaring Uninitialized Variables.   In the following code, the assignment statement X = zeros(1,N) not only declares X to be a 1-by-5 vector of real doubles, but also initializes each element of X to zero.

function X = fcn %#eml

N = 5;
X = zeros(1,N);
for i = 1:N
   if mod(i,2) == 0
      X(i) = i;
   else
      X(i) = 0;
   end
end

This forced initialization creates an extra copy in the generated code. To eliminate this overhead, use eml.nullcopy in the declaration of X:

function X = fcn2 %#eml

N = 5;
X = eml.nullcopy(zeros(1,N));
for i = 1:N
   if mod(i,2) == 0
      X(i) = i;
   else
      X(i) = 0;
   end
end

Declaring Persistent Variables

Persistent variables are local to the function in which they are declared, but they retain their values in memory between calls to the function. To declare persistent variables in Embedded MATLAB functions, use the persistent statement, as in this example:

persistent PROD_X;

The declaration should appear at the top of the function body, after the header and comments, but before the first use of the variable.

See Also.  

Initializing Persistent Variables

Initializing Persistent Variables

You initialize persistent variables in Embedded MATLAB functions the same way as in MATLAB (see Persistent Variables in the MATLAB Programming Fundamentals documentation). When you declare a persistent variable, Embedded MATLAB initializes its value to an empty matrix. After the declaration statement, you can assign your own value to it using the isempty statement, as in this example:

function findProduct(inputvalue)
persistent PROD_X

if isempty(PROD_X)
   PROD_X = 1;
end
PROD_X = PROD_X * inputvalue;

Supported Variable Types

Embedded MATLAB functions support a subset of MATLAB data types:

Type

Description

char

Character array (string)

complex

Complex data. Cast function takes real and imaginary components

double

Double-precision floating point

int8, int16, int32

Signed integer

logical

Boolean true or false

single

Single-precision floating point

struct

Structure (see Using Structures)

uint8, uint16, uint32

Unsigned integer

Fixed-pointSee Working with the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox User's Guide documentation.

Using Matrix Indexing Operations

The Embedded MATLAB subset supports matrix indexing operations for a matrix M with limitations for the following types of expressions:

Working with Complex Numbers

The Embedded MATLAB subset supports complex numbers and operations.

Creating Local Complex Variables By Assignment

As in MATLAB, you create complex variables by assignment. Unlike in MATLAB, you must set complexity at the time of assignment, either by assigning a complex constant to the variable or using the complex function, as in these examples:

x = 5 + 6i; % x is a complex number by assignment.
y = 7 + 8j; % y is a complex number by assignment.
x = complex(5,6); % x is the complex number 5 + 6i.

Use the following rules to specify and use complex variables in Embedded MATLAB functions:

Semantic Rules for Complex Numbers

with the following exceptions:

Working with Characters

The Embedded MATLAB subset represents characters in 8 bits and, therefore, does not support the complete set of Unicode® characters. Because many mathematical operations require more than 8 bits of precision, it is recommended that you do not perform arithmetic with characters in Embedded MATLAB code.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS