Skip to content
MathWorks - Mobile View
  • MathWorks アカウントへのサインインMathWorks アカウントへのサインイン
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
MathWorks
  • 製品
  • ソリューション
  • アカデミア
  • サポート
  • コミュニティ
  • イベント
  • MATLAB を入手する
  • MathWorks アカウントへのサインインMathWorks アカウントへのサインイン
  • Access your MathWorks Account
    • マイ アカウント
    • コミュニティのプロファイル
    • ライセンスを関連付ける
    • サインアウト

ビデオ・Webセミナー

  • MathWorks
  • ビデオ
  • ビデオ ホーム
  • 検索
  • ビデオ ホーム
  • 検索
  • 営業へのお問い合わせ
  • 評価版
4:04 Video length is 4:04.
  • Description
  • Full Transcript
  • Related Resources

Generating Object-Oriented C++ Code from MATLAB Algorithms

Learn how you can use MATLAB Coder™ to generate object-oriented C++ code from MATLAB® classes.

  • Generate C++ Classes from MATLAB Classes: MATLAB Coder produces C++ classes from classes in your MATLAB code, including value classes, handle classes, and system objects. The generated code can be compiled into C++ libraries or executables and can be integrated into your existing C++ source code.
  • Generate C++ Code with Namespaces: MATLAB Coder can generate C++ code in a namespace, making it easy to integrate with other source code that might have identical function or data type names. The code generator packages all generated functions and type definitions into the namespace.
  • Use Dynamically Allocated C++ Arrays in Generated Function Interfaces: Generate C++ code for MATLAB functions that accept or return an array with an array size unknown at compile time, or whose bound exceeds a predefined threshold. In the generated code, memory for the array is dynamically allocated and implemented as a class template named coder::array. In addition to exception-safe memory deallocation, coder::array provides APIs to access and manage the dynamic array.

Intro

In this demonstration, I'm going to use MATLAB Coder to generate production C++ code from MATLAB algorithms. We've made a number of improvements to our C++ code generation and I want to highlight these in this video.

The MATLAB example I'm going to use represents oscillating systems. <Show a picture of Live Editor with the system's output>

Open app and setup

<Open App and select entry point in the background>

I'm going to generate some C++ classes from these MATLAB classes using the MATLAB Coder app. Everything I'll show today is also available from the command line using the codegen command.

The entry point is effectOfDamping. I need to tell MATLAB Coder the types of inputs the function needs. We can run the test script runEffectOfDamping to automatically detect my types.

Check for issues

I'll also use the same test script to check for run-time issues prior to generating my C++ code. <Check for issues in the background>

Code generation

I'm going to generate a DLL and C++ to share this code with my team. Choosing C++ shows an option added in R2019b called "Interface style" where you can choose an interface with free functions or an interface using a class and methods on that class. I'm going to choose "methods" to get more idiomatic C++ and I'll name the class OscillatorInterface so we can find it in the generated code. <Generate code now>

Looking at generated code

To explore the generated code, let's open the MATLAB Coder Report. On the left, we see our input MATLAB code and the generated C++ code.

Encapsulated class

Let's take a look at our interface class resulting from that "interface style" option. The class is exported from the DLL, handles initialization and cleanup, and has our entry-point function as a public method. Any MATLAB persistent and global variables are mapped to class properties to make the generated code multi-instance.

MATLAB classes to C++ classes

Starting in R2020a MATLAB classes are mapped to C++ classes. R2020b adds the ability to map MATLAB packages to C++ namespaces. The generated C++ class mimics as many of the MATLAB class's attributes as possible.

coder::array

In R2020a the generated code uses the coder::array class to manage dynamically allocated arrays. coder::array has an interface similar std::vector, provides indexing operator overloads, and ensures exception-safe memory deallocation.

SIL and debugging

Let's verify the behavior of our code. With Embedded Coder, <Select the test script and enable debugging> I can use SIL debugging to open the generated code in a debugger. <Run in the background> The code starts up in a separate process and that process is opened in Visual Studio for debugging. MATLAB Coder passes data from the script to the generated code.

Package

Once we're happy with the behavior of the generated code, I'll package the code for my team in a ZIP file.

Later, if you want to transition from the App to a command line workflow for automated builds, choose the "Convert to script" option to get a script version of your project.

Performance/Compatibility

We've been continually improving MATLAB Coder. Since R20xx, we've added support for numerous MATLAB capabilities, including Table, DateTime, Timetable, and support for machine learning and deep learning. We strive to generate fast code that approaches or even exceeds hand code.

Finalize

If you're interested in C++ code generation from MATLAB code I suggest taking a look at the new C++ capabilities in MATLAB Coder as of R2020b. Taken together, they allow for the generation of idiomatic and exception-safe C++ code using MATLAB Coder. The example code used in this video is available in the product documentation. Thanks for watching!

Related Products

  • MATLAB Coder

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Feedback

Featured Product

MATLAB Coder

  • Request Trial
  • Get Pricing

Up Next:

4:34
Generating C Code from MATLAB Code

Related Videos:

4:19
Unit Testing C Code Using MATLAB and MATLAB Coder
47:38
MATLAB to C/C++ Made Easy
2:58
Unit Testing Your Generated Code Using MATLAB Coder
4:17
Integrate Code into Visual Studio Using MATLAB Coder

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • 営業へのお問い合わせ
  • 評価版

MathWorks

Accelerating the pace of engineering and science

MathWorksはエンジニアや研究者向け数値解析ソフトウェアのリーディングカンパニーです。

ディスカバー…

製品を見る

  • MATLAB
  • Simulink
  • 学生向けソフトウェア
  • ハードウェア サポート
  • File Exchange

製品評価版の入手または製品の購入

  • ダウンロード
  • 評価版ソフトウェア
  • 営業へのお問い合わせ
  • 価格とライセンス
  • MathWorksストア

使い方を学ぶ

  • ドキュメンテーション
  • チュートリアル
  • 例
  • ビデオ・Webセミナー
  • トレーニング

サポートを受ける

  • インストールのヘルプ
  • MATLAB Answers
  • 技術コンサルティング
  • ライセンスセンター
  • サポートへのお問い合わせ

MathWorks について

  • 採用情報
  • ニュースルーム
  • 社会貢献
  • ユーザー事例
  • MathWorks について
  • Select a Web Site United States
  • トラストセンター
  • 商標
  • プライバシー ポリシー
  • 違法コピー防止
  • アプリケーション ステータス

© 1994-2022 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

MATLAB を語ろう