MathWorks - Mobile View
  • MathWorks 계정에 로그인합니다.MathWorks 계정에 로그인합니다.
  • Access your MathWorks Account
    • 내 계정
    • 나의 커뮤니티 프로필
    • 라이선스를 계정에 연결
    • 로그아웃
  • 제품
  • 솔루션
  • 아카데미아
  • 지원
  • 커뮤니티
  • 이벤트
  • MATLAB 다운로드
MathWorks
  • 제품
  • 솔루션
  • 아카데미아
  • 지원
  • 커뮤니티
  • 이벤트
  • MATLAB 다운로드
  • MathWorks 계정에 로그인합니다.MathWorks 계정에 로그인합니다.
  • Access your MathWorks Account
    • 내 계정
    • 나의 커뮤니티 프로필
    • 라이선스를 계정에 연결
    • 로그아웃

비디오 및 웨비나

  • MathWorks
  • 비디오
  • 비디오 홈
  • 검색
  • 비디오 홈
  • 검색
  • 영업 상담
  • 평가판 신청
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

Generating C Code from MATLAB Code

Bill Chou, MathWorks

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

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
0:26
Deploying to iPhone and iPad Apps 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: .

Select web site

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

  • 영업 상담
  • 평가판 신청

제품 소개

  • MATLAB
  • Simulink
  • 학생용 소프트웨어
  • 하드웨어 지원
  • File Exchange

다운로드 및 구매

  • 다운로드
  • 평가판 신청
  • 영업 상담
  • 가격 및 라이선스
  • MathWorks 스토어

사용 방법

  • 문서
  • 튜토리얼
  • 예제
  • 비디오 및 웨비나
  • 교육

지원

  • 설치 도움말
  • 사용자 커뮤니티
  • 컨설팅
  • 라이선스 센터
  • 지원 문의

회사 정보

  • 채용
  • 뉴스 룸
  • 사회적 미션
  • 영업 상담
  • 회사 정보

MathWorks

Accelerating the pace of engineering and science

MathWorks는 엔지니어와 과학자들을 위한 테크니컬 컴퓨팅 소프트웨어 분야의 선도적인 개발업체입니다.

활용 분야 …

  • Select a Web Site United States
  • 특허
  • 등록 상표
  • 정보 취급 방침
  • 불법 복제 방지
  • 매스웍스코리아 유한회사
  • 주소: 서울시 강남구 삼성동 테헤란로 521 파르나스타워 14층
  • 전화번호: 02-6006-5100
  • 대표자 : 이종민
  • 사업자 등록번호 : 120-86-60062

© 1994-2021 The MathWorks, Inc.

  • Naver
  • Facebook
  • Twitter
  • YouTube
  • LinkedIn
  • RSS

대화에 참여하기