2016年9月14日 星期三

Matlab Coder-More Than One Function Files

The following situations may appear when we build/run the project.
  1. If we convert more than one Matlab function and some of the files generated by the Coder happen to have the same source and header file name, then we should change some of the file names by ourselves to make them different so as to prevent wrong results. After this, we must rewrite some of header file(s) and source file(s) contents. For example, if we have two Matlab functions files A.m and B.m to convert, each function may generate a name C/C++ file called "power.c." Despite their having the same file name, their contents are different. It is only of the Matlab Coder's internal design that they are given the same filename. Since one project cannot have two functions with the same file name, we can rename "power.c" to "Apower.c" for the A function and "Bpower.c" for the B function to differentiate them.
  2. When the error message: 'error LNKxxxx: "statement" already defined in OOO.obj' appears, it indicates repeated definitions in one project. We need to remove source files with repeated contents (even if they have different file names) and retain only one copy.
An easy way to prevent the above problems is converting the function files together. We can add all functions and set their data types and variable sizes, respectively. This avoids to cause to run into the above situations.
We show an example as below.
In Xcode domain, we should include both function head files and one xxx_emxAPI.h. In our case, xxx called add.
The results are:
翼はいならい:

Matlab Coder-Infinite Array Size and Complex Value

In here, we introduce infinite array size with complex value, which is more complicated than nature values. Also, we can go to xxx_emxAPI.h to see the declarations and rtwtypes.h/xxx_types.h to check their data definitions. We show the structure for single-precision floating-point with complex value below.
We can follow the steps to rewrite the main program, which is shown below.
Finally, we can see the result as below.

2016年9月12日 星期一

Matlab Coder-Infinite Array Size

In here, we introduce infinite array size instead of fixed. If we set the data types to Inf. array size, the coder will enforce to use dynamic memory allocation. We will see that the generated files may have many unfamiliar data types such as creal32_T and real32_T. These types are defined by the system, and we can go to a generated file called rtwtypes.h to check their definitions. If the size is set to infinite, we will see another definition file called XXX_types.h, where XXX is the file name.This file declare/define many methods to allocate memory, and we should choose one to use (depending on the data types).

Example
Matlab function is shown below:
In this example, the data type is complex single precision and the size is 1 x infinite vector.

The main program (Xcode):
The emxArray_real32_T *a = emxCreate_real32_T(1,SIZE) means memory allocation, which is declared in XXX_types.h. Note that different data types have different declarations or definitions. For example, if the intended data type is double and complex, then we should replace real_32 T with creal_T (see rtwtypes.h). 

Notice: We should at least include two head files, wise_product.h and wise_product_types.h before building the project.

The result is shown below:


2016年9月11日 星期日

Matlab Coder - Change the Array Size

We had introduced the concept of Matlab Coder Toolbox. In here, we introduce how to change the array size. If we set the array size to a fixed number, the generated C or C++ code is fixed, too (For static memory allocation, we cannot set flexible array size. I will introduce dynamic memory allocation later, which can set array size with flexible)

We go to modify the header and source files, the modified files are shown below (data type to pointer).









The main program is shown below (now we can change array size arbitrarily), the result would be correctly:
I will introduce dynamic memory allocation and complex value later.

Matlab Coder Features and Associated Applications.

In here, I will introduce the properties of Matlab Coder Toolbox and associated applications.

Matlab Coder is a Matlab toolbox that can be used to generate readable and portable C and C++ code from Matlab code. It supports most of the Matlab language and a wide range of Matlab toolboxes. One can integrate the generated code into his/her projects as source code, static libraries, or dynamic libraries. I use the following Matlab version and coder version: R2014b / Matlab Coder 2.7. 


Some features of Matlab Coder are

  • ANSI/ISO compliant C and C++ code generation. 
  • Code generation support for some Matlab toolboxes, including the Communications System Toolbox, Computer Vision System Toolbox, DSP System Toolbox, Image Processing Toolbox, and Signal Processing Toolbox. 
  • MEX function generation for code verification and acceleration. 
  • Legacy C code integration into Matlab algorithms and generated code. 
  • Multicore-capable code generation using OpenMP. 
  • Static or dynamic memory-allocation control. 
  • Application and equivalent command-line functions for managing code generation projects.

Before Using Matlab Coder

Before using the Matlab Coder, one needs to do and know several things:

  1. Install Matlab with Matlab Coder Toolbox on PC.
  2. Because Matlab Coder can convert Matlab functions only, we need to have at least one Matlab function file.
  3. In using the Matlab Coder Toolbox for the first time, the user may be asked to setup a compiler by system. We can type "mex -setup" in the Matlab command window and follow its steps to setup the compiler.

Generating Code from MATLAB
To generate C/C++ code using the Matlab Coder, we may do the following:
  1. Open the Matlab Coder window. For this, we can type "coder" in the Matlab command window or go to APPS and select Matlab Coder on the top bar.
  2. Enter project name and location, then click the OK button.
  3. After the above step, we need to add a Matlab function file and dene its data types and variable sizes.
  4. Then we go to the build tab. Under this tab, we can select a suitable output type. Below the "Generate code only" option block, we can see more settings link, and from it we can set customized environment. We can go to all settings button to choose C/C++ language. If you want to test the generated code with Microsoft Visual Studio, then you need to choose the C++ language. If one wants to run the code with the Mac Xcode, you can choose C or C++ languages. After finishing the setting, we can click the build button, and then the system will run automatically.
  5. Under the all settings, we can choose different code replacement library (hardware) such as C89/C90 (ANSI) and TI series. After changing the code replacement library, we need to change the device vendor to specified one. In here, we choose the C89/C90 (ANSI) to demonstrate later.
  6. After a successful build, you will see a build finished window. In build failure, we would see some error messages in the build window. Then we can click "view report" link to see the whole generated C/C++ code files. Now, we can test the C/C++ code on the specified compiler.

Simulate the Generated C++ Code with Mac Xcode/ Windows Microsoft Visual Studio
This section describes how we can test our generated C or C++ code from Matlab Coder. We can build and run the programs with Mac Xcode/ Windows Microsoft Visual Studio and compare it with Matlab results. In here, I use Mac Xcode to demonstrate, and supplement some information when the user use Microsoft Visual Studio.

Because Matlab Coder can only convert Matlab "functions," so we should write the main program by ourselves. The following figure is an example of Matlab function. The two inputs are a and b, the output is c.
 
Now, we can use this function to generate the C++ code. Set two inputs are single-precision floating-point array.
Go to the Build tab, we select output type is C/C++ Static Library then choose Generate code only. We can go to More settings to set advanced setting. In the memory item, we can select static or dynamic memory allocation. In code appearance, we can choose Matlab source code a comments to see the Matlab source in C++ file(s).  In all settings, we can go to language part and select C++.
After all setting, then we can click Build to build the C++ code. The following is the build finish window. Further, we can click View report to see the generated files. Now, we can use specified compiler to execute our generated code from Matlab Coder.
The following steps can be followed to run the generated C++ code with Xcode.

  • Create a new Xcode project.
  • Select Command Line Tool.
  • Enter the project name and choose C++ language then click create.
  • Add all source and header files to the project.
  • Include all generated head file(s).

The following steps can be followed to run the generated C++ code with Microsoft Visual Studio.
  • Open the Microsoft Visual Studio and create a new project.
  • Select Win32 Console Application and enter project name, then click OK button. 
  • Copy all source and header files to the project file path.
  • Add all source and header files to the project.
  • Include all generated head file(s)
  • Include "stdafx.h" in all source files.
The main program is shown below. We can build and run to see the result correctly or not.
The result is shown below and correctly.


We will introduce other data types.