MATLAB Code Generation with codegen

Overview

The codegen command generates optimized C/C++ code from MATLAB® functions and builds the resulting code into executables or libraries. This tool is essential for:

  • Accelerating MATLAB algorithms
  • Deploying MATLAB code to embedded systems
  • Integrating MATLAB logic with other programming languages

Key Features

  • Supports multiple build targets (MEX, static/dynamic libraries, executables)
  • Handles various input types (fixed-size, variable-size, global variables)
  • Enables single-precision and fixed-point code generation
  • Provides optimization controls (inlining, OpenMP parallelization)

Basic Syntax

matlab

Copy

codegen options function -args {func_inputs}

Common Use Cases

1. Generating a MEX Function

matlab

Copy

% Function with input validation
function y = mcadd(u,v)
    arguments
        u (1,4) double
        v (1,1) double
    end
    y = u + v;
end

% Generate MEX
codegen mcadd

2. Creating a Static Library

matlab

Copy

% Generate C static library
codegen -config:lib mcadd -args {zeros(1,4),0}

3. Multi-Signature Support

matlab

Copy

% Generate MEX supporting multiple input types
codegen -config:mex myAdd -args {1,2} -args {int8(2),int8(3)} -report

Advanced Capabilities

Custom Build Configuration

matlab

Copy

cfg = coder.config('lib');  % Library configuration
cfg.TargetLang = 'C++';     % Set target language
codegen -config cfg myFunction -args {myInputs}

Fixed-Point Conversion

matlab

Copy

fixptcfg = coder.config('fixpt');
fixptcfg.TestBenchName = 'my_test';
codegen -float2fixed fixptcfg -config:lib myFunction

Global Variable Handling

matlab

Copy

codegen -globals {'g', 5} myFunction -args {0}

Input Specifications

Supported Input Types

TypeExample
Fixed-size-args {ones(3,3)}
Variable-size-args {coder.typeof(1,[Inf,Inf])}
Enumerations-args {coder.typeof(myEnum.Value)}
Fixed-point-args {fi(4.0,numerictypeObj)}

Output Options

Build Targets

OptionOutput
-config:mexMEX function
-config:libStatic library
-config:dllDynamic library
-config:exeStandalone executable

Output Control

  • -d out_folder: Specify output directory
  • -o output_name: Set output filename
  • -report: Generate code generation report

Optimization Controls

matlab

Copy

% Enable OpenMP parallelization
codegen -O enable:openmp myFunction

% Disable function inlining
codegen -O disable:inline myFunction

Limitations

  • Cannot generate code directly from scripts (must use functions)
  • Doesn’t support generation from private/class folders
  • Some options unavailable for namespaced functions

Best Practices

  1. Always validate MEX outputs against original MATLAB functions
  2. Use -report to analyze generated code
  3. Consider fixed-point conversion for embedded targets
  4. Package builds with -package for portability

🔔🔔  Follow us on LinkedIn  🔔🔔

Related Posts
Salesforce OEM AppExchange
Salesforce OEM AppExchange

Expanding its reach beyond CRM, Salesforce.com has launched a new service called AppExchange OEM Edition, aimed at non-CRM service providers. Read more

The Salesforce Story
The Salesforce Story

In Marc Benioff's own words How did salesforce.com grow from a start up in a rented apartment into the world's Read more

Salesforce Jigsaw
Salesforce Jigsaw

Salesforce.com, a prominent figure in cloud computing, has finalized a deal to acquire Jigsaw, a wiki-style business contact database, for Read more

Service Cloud with AI-Driven Intelligence
Salesforce Service Cloud

Salesforce Enhances Service Cloud with AI-Driven Intelligence Engine Data science and analytics are rapidly becoming standard features in enterprise applications, Read more

author avatar
get-admin