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:
- Install Matlab with Matlab Coder Toolbox on PC.
- Because Matlab Coder can convert Matlab functions only, we need to have at least one Matlab function file.
- 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:
- 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.
- Enter project name and location, then click the OK button.
- After the above step, we need to add a Matlab function file and dene its data types and variable sizes.
- 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.
- 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.
- 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.
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).
- 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 result is shown below and correctly.
We will introduce other data types.
沒有留言:
張貼留言