Guide for Dummies: from MATLAB to C++ through the MATLAB Coder - Sofia Belloni - E-Book

Guide for Dummies: from MATLAB to C++ through the MATLAB Coder E-Book

Sofia Belloni

0,0
5,49 €

oder
-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Nowadays, many of the tools and applications used in the biomedical field are developed in MATLAB (The MathWorks, Inc., MA, USA). However, the C++ code is license-free so better suits the policy of code distributed in the form of Open Source. To facilitate the conversion from MATLAB to C++, MATLAB developers have recently created a toolbox called MATLAB Coder containing various functions and tools to facilitate a (semi-) automatic code conversion. In this book, using significant examples of increasing complexity, we show how to quickly and easily exploit the MATLAB Coder to create ".lib" static libraries, ".exe" executables and ".cpp" source code starting from MATLAB ".m" functions. SINOSSI Al giorno d'oggi, molti dei tools ed applicativi utilizzati in campo biomedicale sono sviluppati in linguaggio MATLAB (The MathWorks, Inc., MA, USA). Tuttavia, il codice C++ non essendo collegato a licenze di utilizzo meglio si adatta alla politica di codice distribuito in forma di Open Source. Per facilitare la conversione dal linguaggio MATLAB al linguaggio C++, gli sviluppatori MATLAB hanno recentemente realizzato un toolbox chiamato MATLAB Coder contenente varie funzioni e strumenti per facilitare la conversione (semi-)automatica del codice. In questo libro, utilizzando esempi significativi di complessità crescente, mostriamo come sfruttare in maniera facile e veloce il MATLAB Coder per realizzare librerie statiche ".lib", eseguibili ".exe" e codice sorgente ".cpp" partendo da funzioni MATLAB ".m".

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



 

INDEX (English version)

 

Abstract (English version)

Introduction

CHAPTER. 1: Materials and Methods

1.1 MATLAB 2020b

1.2 Visual Studio 2019

1.3 MATLAB CoderTM 5.1

1.4 Souce code, static libraries, executables

1.5 License and repository

1.6 Library C++ OpenCV 4.5.1

1.7 Structures for the MATLAB Coder

1.8 From cv::Mat to coder::array and vice versa

CHAPTER. 2: Example-A from MATLAB to C: function add double

2.1 Static library “DummyFunction2doubles.lib”

2.2 Source code “DummyFunction2doubles.c”

2.3 Executable “DummyFunction2doubles.exe”

CHAPTER. 3: Example-B from MATLAB to C: function image (easy) processing

3.1 Static library “DummyFunctionImage.lib”

3.2 Source code “DummyFunctionImage.cpp”

3.3 Executable “DummyFunctionImage.exe”

CHAPTER. 4: Example-B from MATLAB to C: function image (complex) processing

4.1: Static library “ComplexFunctionImage.lib”

4.2 Source code “ComplexFunctionImage.cpp”

3.3 Executable “ComplexFunctionImage.exe”

CHAPTER. 5: Conclusions

SYNOPSIS

 

INDICE (versione Italiana)

 

Abstract (versione Italiana)

Introduzione

CAP. 1: Materiali e Metodi

1.1 MATLAB 2020b

1.2 Visual Studio 2019

1.3 MATLAB CoderTM 5.1

1.4 Codice sorgente, librerie statiche, file eseguibili

1.5 Licenza e repository file

1.6 Libreria C++ OpenCV 4.5.1

1.7 Strutture dati MATLAB Coder

1.8 Da cv::Mat a coder::array e viceversa

CAP. 2: Esempio-1 da MATLAB a C: funzione somma double

2.1 Generazione libreria statica “DummyFunction2doubles.lib”

2.2 Generazione codice sorgente “DummyFunction2doubles.c”

2.3 Generazione file eseguibile “DummyFunction2doubles.exe”

CAP. 3: Esempio-2 da MATLAB a C: funzione processing semplice immagine

3.1 Generazione libreria statica “DummyFunctionImage.lib”

3.2 Generazione codice sorgente “DummyFunctionImage.cpp”

3.3 Generazione file eseguibile “DummyFunctionImage.exe”

CAP. 4: Esempio-3 da MATLAB a C: funzione processing complesso immagine

4.1: Generazione libreria statica “ComplexFunctionImage.lib”

4.2 Generazione codice sorgente “ComplexFunctionImage.cpp”

3.3 Generazione file eseguibile “ComplexFunctionImage.exe”

CAP. 5: Conclusioni

SINOSSI

English and Italian Book

Guide for Dummies: from MATLAB to C++ through the MATLAB Coder

Guida per Dummies: da MATLAB a C++ attraverso il MATLAB Coder

Titolo | Guide for Dummies: from MATLAB to C++ through the MATLAB Coder

Autore | Filippo Piccinini – Sofia Belloni

ISBN | 979-12-20342-12-4

© 2021 - Tutti i diritti riservati all’Autore

Questa opera è pubblicata direttamente dall'Autore tramite la piattaforma di selfpublishing Youcanprint e l'Autore detiene ogni diritto della stessa in maniera esclusiva. Nessuna parte di questo libro può essere pertanto riprodotta senza il preventivo assenso dell'Autore.

Youcanprint

Via Marco Biagi 6 - 73100 Lecce

www.youcanprint.it

[email protected]

Abstract (English version)

Nowadays, many of the tools and applications used in the biomedical field are developed in MATLAB (The MathWorks, Inc., MA, USA). However, C++ code is more versatile and, since it is licence-free, it suites better with the policy of code distributed in the form of Open Source.

Having user-friendly tutorials to quickly convert MATLAB code into C++ code is a hot topic for applications in several fields. To facilitate the conversion from MATLAB to C/C++, the MATLAB developers have recently created a toolbox called MATLAB Coder toolbox containing functions and tools to facilitate a (semi-)automatic code conversion.

In this book, using significant examples of increasing complexity, we have described how to exploit the MATLAB Coder to create static “.lib” libraries, “.exe” executables and “.cpp” source code starting from MATLAB “.m” functions. In particular, starting from some case studies based on simple functions able to manage numerical inputs, we have shown how to manage various parameters of the MATLAB Coder toolbox, in order to be able to convert even complex functions based on image processing algorithms.

Abstract (versione Italiana)

Al giorno d’oggi, molti dei tools ed applicativi utilizzati in campo biomedicale sono sviluppati in linguaggio MATLAB (The MathWorks, Inc., MA, USA). Tuttavia, il codice C++ risulta essere più versatile e, non essendo collegato a licenze di utilizzo, meglio si adatta alla politica di codice distribuito in forma di Open Source.

Poter disporre di protocolli user friedly per convertire in breve tempo codice MATLAB in codice C++ è un hot topic per applicazioni in vari settori. Per facilitare il passaggio dal linguaggio MATLAB al linguaggio C/C++, gli sviluppatori MATLAB hanno recentemente realizzato un toolbox chiamato MATLAB Coder contenente varie funzioni e strumenti per facilitare la conversione (semi-)automatica del codice.

In questo libro, utilizzando esempi significativi di complessità crescente, mostriamo come sfruttare il MATLAB Coder per realizzare librerie statiche “.lib”, eseguibili “.exe” e codice sorgente “.cpp” partendo da codice MATLAB “.m”. In particolare, partendo da casi di studio basati su funzioni semplici in grado di gestire input numerici, abbiamo mostrato come sfruttare i vari parametri del toolbox MATLAB Coder, per riuscire poi a convertire funzioni anche complesse basate su algoritmi di analisi di immagini.

Attualmente in letteratura non sono presenti molti manuali create per guidare passo-passo possibili interessati nell’utilizzo del MATLAB Coder. Speriamo quindi che questo documento possa essere un valido aiuto per gli sviluppatori interessati a convertire in maniera facile e veloce codice MATLAB in codice C++.

Introduction

Nowadays, MATLAB is one of the most popular programming environments, especially among engineers and researchers working in the health field. In particular, MATLAB represents a powerful programming language optimized for mathematical computation based on multidimensional matrices. Thanks to these characteristics, it is one of the most common languages in the biomedical world where it is used for the development of several tools and applications, especially in prototype form. However, it is not a licence-free environment and the need to have a license to use MATLAB represents a limit for the dissemination and use in the community of the developed code.

On the other hand, C++ is an open source-code that does not require any license for development and distribution. For this reason it is widely spread in the community although it requires greater programming skills in the code development stage. Being able to quickly and easily convert MATLAB code into C++ code may therefore be very interesting for applications in many fields, considering that C and C++ are among the most popular open source programming languages ​​used in the industry. However, since MATLAB and C++ are two syntactically and conceptually very different languages, a possible step-by-step manual conversion of the MATLAB algorithm would present various problems related first of all to the introduction of potential errors during the conversion process, which moreover would be also a very time-consuming task. Furthermore, it will be necessary to maintain two separate implementations of the algorithm not automatically synchronized with each other.

To facilitate a (semi-)automatic code conversion, the MATLAB developers have recently developed the MATLAB Coder™ toolbox containing several functions and tools to help the code conversion and build static ".lib" libraries, ".exe" executable files, or code. source ".cpp". The possibility of being able to convert (semi-)automatically the code could be very appreciated. The MATLAB Coder may therefore be a valuable aid for developers engaged in both research and the industrial world.

In this book, we analyse some of the many opportunities offered by the MATLAB Coder toolbox to be able to easily and quickly convert MATLAB code into C++ code with the aim of creating a step-by-step guide to help those interested in using the MATLAB Coder toolbox. In particular, using case studies of increasing difficulty, all the steps to obtain static libraries, executables and C++ source code starting from MATLAB code will be explained and illustrated.

This book is organised in the following chapters:

• Chapter1: description of the Materials and Methods, with a digression on the versions used of MATLAB, Microsoft Visual Studio, MATLAB Coder and OpenCV library.

• Chapter2: first step-by-step example to create a static library ".lib", an executable file ".exe" and source code ".cpp" starting from the MATLAB ".m" code of a function that adds two double numbers.

• Chapter3: second step-by-step example to create a static library ".lib", an executable file ".exe" and source code ".cpp" starting from the MATLAB ".m" code of a function that accepts an image as input and returns it in output after adding a constant value to the intensity of each pixel of the image.

• Chapter4: third step-by-step example to create a static library ".lib", an executable file ".exe" and source code ".cpp" starting from the MATLAB ".m" code of a function that accepts an image as input and processes it identifying the outlines of the objects represented in it using the classic Sobel algorithm.

• Chapter5: conclusions with particular emphasis on the importance of creating a step-by-step guide to quickly convert MATLAB code to C++ code.

 

CHAPTER. 1: Materials and Methods

 

All the analyses carried out in this work have been performed using an entry-level consumer laptop, Windows 64-bit, AMD Ryzen 5 processor, with Radeon Vega 8 video card. The main software used were:

• MATLAB 2020b

• Visual Studio 2019

• MATLAB Coder™ version 5.1

In the following sub-chapters, the main characteristics of these tools will be described in detail and some basic notions on C++ and MATLAB image management will be illustrated.

 

1.1 MATLAB 2020b

 

MATLAB, a short name standing for MATrix LABoratory, is a commercial programming environment optimized for scientific applications, numerical processing and for the simulation of dynamic systems. The term MATLAB also directly refers to the homonymous programming language created by MathWorks (The MathWorks, Inc., Massachusetts, USA). Today MATLAB is one of the most widespread scientific development environments in the biomedical world and is used in many university courses and companies in the engineering field because thanks to the many toolboxes (i.e. collection of programs and functions that allow the solution of problems at specific fields) it is very versatile and simple to use. The 2020b release, used in this text, was officially released on September 16, 2020.

 

Fig. 1.1: MATLAB Logo (copyright MathWorks).

 

 

1.2 Visual Studio 2019

 

Visual Studio (Microsoft, Washington, USA) is one of the leading integrated development environments (IDEs) for C and C++ languages. Today, the latest version available is the 2019, available for Windows in three editions: Community, Professional and Enterprise. In this work, the Community version was used, which can be used free of charge by the whole community without any need for a license.

 

Fig. 1.2: Visual Studio 2019 Logo (copyright Microsoft).

 

 

1.3 MATLAB CoderTM 5.1

 

The MATLAB Coder allows to generate C and C++ code from MATLAB code. The generated code can then be easily integrated into C/C++ projects in form of source code, static libraries and executable files. The MATLAB Coder supports almost all the MATLAB language and a wide range of toolboxes. However, not all MATLAB functions and commands are supported by the MATLAB Coder (for example, the "clear" command to clean the workspace is not supported) and an intense phase of debugging of the MATLAB code is often necessary before proceeding with the conversion. It is also possible to include the generated code as a MEX function to use it in MATLAB itself. the version 5.1 of the MATLAB Coder was the one used in this work. Today, there are no many comprehensive guides in the literature to understand step-by-step how to efficiently exploit the functions and opportunities offered by the MATLAB Coder.

 

Fig. 1.3: https://it.mathworks.com/products/matlab-coder.html (copyright MathWorks).

 

Hereafter we describe some elements that will be used in the examples illustrated in the following chapters. In particular:

• “%#codegen”: it is a directive for the compilation that can be indicated after the declaration of the function, precisely in the same line, to indicate that you intend to generate code in a different language for that specific source function. In this way, the MATLAB Code Analyzer is able to help the user to correct errors that may arise during code generation, for example by informing the user in case of functions not supported by the MATLAB Coder. This directive would not be necessary for MATLAB Function Blocks (code blocks mainly used in Simulink, dedicated to a single function and typically saved in a single file with the same name as the function), because the code inside them is always optimised for the generation of C/C++ code. In fact, in this case, the presence of "% # codegen" simply does not change the behaviour of the error checking.

• “assert(cond)”: it is a standard MATLAB function that throws an error if the cond condition is evaluated as false. It is possible to use the "assert" function to define controls on the inputs passed to the function or other characteristics of the function for which you intend to generate C/C++ code.

 

On regards to the license of the code generated using the MATLAB Coder, we show what is mentioned on the MathWorks website (printscreen saved on 06/04/2021):

 

Fig. 1.4: https://it.mathworks.com/products/matlab-coder.html (copyright MathWorks).

 

Precisely: “The generated code is royalty-free: distribute it for free to your customers for commercial applications”. However, it is important to note that most of the files generated by the MATLAB Coder are protected by the following license which allows free use for educational and research purposes, but limits use for commercial purposes.

/*

* Academic License - for use in teaching, academic research, and meeting

* course requirements at degree granting institutions only. Not for

* government, commercial, or other organizational use.

*/