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

ビデオ・Webセミナー

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

Generating C Code from MATLAB Code

Generate readable and portable C and C++ code from MATLAB® code, including over 1,200 functions used for applications ranging from image processing and computer vision to advanced DSP and communications systems development, using MATLAB Coder™.

This video shows the workflow of generating C code through the use of a Kalman filter designed in MATLAB.

Learn the difference between using MATLAB Coder and MATLAB Compiler™.

MATLAB Coder generates readable and portable C code from your MATLAB algorithms. This automated approach speeds up your design workflow and eliminates coding errors introduced by a manual translation process.

MATLAB Coder supports most of the MATLAB language and over 1,200 functions from a wide range of toolboxes used for applications ranging from image processing and computer vision to advanced DSP and communications systems development. To illustrate the workflow, I'm going to use a Kalman filter as an example numerical algorithm.

The Kalman filter DOTM file that you see here is slightly modified from the version that ships with MATLAB Coder. In addition, I have a simple test bench which gets the input, runs the Kalman filter, and plots it on the graph. While I have two files, I'm only interested in generating code for the algorithm.

Coming back to the algorithm, notice that I'm pre-allocating matrices like A and H. This is one of the tricks that helps generate better code. To start off, make sure you have the percentage pound cogent pragma at the top of your file. The pragma enables additional co-generation checks in the code analyzer, which is a tool built into the MATLAB editor.

Taking a look at the code analyzer at the top right corner, we see that it's flagged a couple of things. The first thing is that we need to complete statements with a semicolon. So let's fix that.

And over here with the brackets it's unnecessary. So we can delete it. Thirdly, MATLAB Coder does not currently support try-catch constructs. So let's remove it for now.

After fixing these things, the code analyzer is green so we're good to go. From here let's go and start the MATLAB Coder app. Here it is, under the co-generation category.

Here's the MATLAB Coder app. And the first thing we're going to do is to tell it which file we want to generate code for. From here, the code generation readiness tool makes an initial assessment and flags things like unsupported functions. If you have no issues, you actually won't see this screen. But in this case the tool is telling us that import data is not supported for co-generation.

We wrote this chunk of code for debugging purposes as we were designing the algorithm. And since we don't need to generate code for this diagnostic code, we can comment it out and move to the next step. Now we need to define our input types. When working with C code we need to explicitly specify the data type and sizes of our inputs.

The easiest way to do this is to use a test bench that exercises the function that we want to generate code for. In this case this is the Kalman filter test bench that I showed you earlier. And if we click on the outer defined input types, MATLAB Coder will run the test bench and infer the input data types and sizes.

It figures out that our input data type is a two-by-one array of type double. So we'll accept that. If you want to, you can also explicitly set the data type and sizes yourself. Moving to the next step, MATLAB Coder is going to check for runtime errors in the generated code.

They'll perform things like array bounds checking and dimension checking to look for violations of memory integrity. In this case, everything passes with flying colors. So we can move on.

Now we're ready to generate code. For our output we can generate source code, MEX files, static or dynamic libraries, or an executable. To keep things simple let's go ahead and generate source code. So we just generate code successfully.

I enabled the option to insert the MATLAB code as comments in the generated C code so that it's easier to trace back. We also have a header file here. At this point there are a couple of things you can do. You can of course create a MEX file and bring it back to MATLAB for acceleration purposes. You can tune some of the generation options related to speed, memory usage, and code appearance and then regenerate code. You can also do some code verification.

Of course, you can simply export the generated code and use it elsewhere. This was a quick overview of generating C code from your MATLAB code. Take a look at our documentation for more examples and other details.

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:19
Unit Testing C Code Using MATLAB and MATLAB Coder

Related Videos:

2:58
Unit Testing Your Generated Code Using MATLAB Coder
4:17
Integrate Code into Visual Studio Using MATLAB Coder
11:24
Addressing Implementation Constraints Using MATLAB Coder
44:41
MATLAB to iPhone Made Easy

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 を語ろう