MathWorks - Mobile View
  • Inicie sesión con su cuenta de MathWorksInicie sesión con su cuenta de MathWorks
  • Access your MathWorks Account
    • Mi Cuenta
    • Mi perfil de la comunidad
    • Asociar Licencia
    • Cerrar sesión
  • Productos
  • Soluciones
  • Educación
  • Soporte
  • Comunidad
  • Eventos
  • Obtenga MATLAB
MathWorks
  • Productos
  • Soluciones
  • Educación
  • Soporte
  • Comunidad
  • Eventos
  • Obtenga MATLAB
  • Inicie sesión con su cuenta de MathWorksInicie sesión con su cuenta de MathWorks
  • Access your MathWorks Account
    • Mi Cuenta
    • Mi perfil de la comunidad
    • Asociar Licencia
    • Cerrar sesión

Vídeos y webinars

  • MathWorks
  • Vídeos
  • Videos Home
  • Buscar
  • Videos Home
  • Buscar
  • Contáctese con ventas
  • Software de prueba
  Register to watch video
  • Description
  • Full Transcript
  • Related Resources

Generate C++ Classes from MATLAB Classes Using MATLAB Coder

Bill Chou, MathWorks

Learn how to generate C++ classes from MATLAB® classes. Write MATLAB classes with public and private properties and methods, and MATLAB Coder™ will generate C++ classes with the equivalent private and public properties and methods. MATLAB classes such as value classes, handle classes, and system objects are supported.

Take your MATLAB classes, define with properties and methods and compile it into C++ classes with the equivalent properties and methods. So here instead of MATLAB, I have a simpleClass. And it's derived from the MATLAB handles class.

This particular class has two inputs, x and y. These are two points in space. And it has one output, z. Down here you will see that this is the initial constructor. And from here you have your set input, which is entering the values of x and y.

You have a get results method which is computing the output. And in the compute method, it's taking the two coordinates x and y and it's computing the distance between them. Once you compute it, you can use the get size x method to get the result.

So here's my actual algorithm. And instead of that, I've instantiated my simpleClass. Then I set the inputs. And from there, I ask it to compute. And finally, I'll get the result of the distance between my two inputs.

In addition, I have a simple test bench that exercises myAlg. And you can see with these two points A and B, the distance between these two points in space is the square root of 2. Since it runs in MATLAB, I can go ahead and generate code. So I'll start the MATLAB coder app.

Inside the MATLAB coder app, the first thing I need to do is to specify the following that I need to generate code for. So that's myAlg in this particular case. And the Code Readiness tool identified an issue that we need to take a look at before we start generating code.

So clicking on this, this is telling us that the try/catch block is not supported for co-generation. So in order to address it, we'll comment out try/catch. The Code Generation Readiness tool says everything's ready to go.

So next I need to define the input data types and sizes. The simple way is to exercise the test bench and asked MATLAB Coder to auto define the input types. So that's all set. Next, it's always a good idea to check for runtime issues. And in this step, MATLAB Coder will generate some trial code, build it, and see if there's any issues. In which case, we don't have any in this particular case.

So now we're ready generate code. We can select on C++ to generate code for our MATLAB class. OK, so we're done. We generated code successfully. Let's take a look at the code generation report. We go ahead and click on my myAlg.cpp.

We can take a look. And the resulting code here is instantiating my simpleClass, initializing it, setting the inputs, running compute method, and then finally returning the get result method. And it mirrors what we see in our MATLAB code pretty closely.

And the actual definition of simple class, you can see the compute method. And it's getting the 2 x and y-coordinates. And then it's computing the distance between them using square root.

Here you can see the get result method, which is just returning the private z variable back. And that was the initialization. Here in the set input method, you can see we're setting the coordinates of x and y. So that was a quick look at taking a MATLAB class and generating the equivalent C++ class with properties and methods using MATLAB coder.

Related Products

  • MATLAB Coder

Learn More

SimpleClass File Exchange
Related Information
Free Quick Start Guide for MATLAB Coder

Feedback

Featured Product

MATLAB Coder

  • Request Trial
  • Get Pricing

Up Next:

2:57
Generate Executable for Prototyping Using MATLAB Coder

Related Videos:

4:19
Unit Testing C Code Using MATLAB and MATLAB Coder
4:34
Generating C Code from MATLAB Code
11:24
Addressing Implementation Constraints Using MATLAB Coder
2:58
Unit Testing Your Generated Code 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

  • Contáctese con ventas
  • Software de prueba

Explorar productos

  • MATLAB
  • Simulink
  • Software para estudiantes
  • Soporte para hardware
  • File Exchange

Probar o comprar

  • Descargas
  • Software de prueba
  • Contáctese con ventas
  • Precios y licencias
  • Cómo comprar

Aprender a utilizar

  • Documentación
  • Tutoriales
  • Ejemplos
  • Vídeos y webinars
  • Formación

Obtener soporte

  • Ayuda para la instalación
  • Respuestas
  • Consultoría
  • Centro de licencias
  • Contactar con soporte

Acerca de MathWorks

  • Ofertas de empleo
  • Sala de prensa
  • Misión social
  • Contáctese con ventas
  • Acerca de MathWorks

MathWorks

Accelerating the pace of engineering and science

MathWorks es el líder en el desarrollo de software de cálculo matemático para ingenieros

Descubra…

  • Select a Web Site United States
  • Patentes
  • Marcas comerciales
  • Política de privacidad
  • Antipiratería
  • Estado

© 1994-2021 The MathWorks, Inc.

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

Únase a la conversación