Matlab Coder with Eclipse IDE (Cygwin GCC)
28 May 2012
15:56
Matlab Coder Workflows
Generating C Code Using MATLAB Coder
Click here to download the example files (including both m and c files). The only one m file is getRand.m to return a random number
Normally distributed pseudorandom numbers.
1. Create a new Matlab Coder Project
(1) From matlab main menu, select File > New > Code Generation Project.
(2) The Matlab Coder Project dialog box opens, enter a name for the project and click OK
(3) A new window 'MATLAB Coder: MEX Function' opens, Click 'Add files' in the 'Overview' Tab to add the m files into the project
(4) Specify the Entry-Point Input Types: Because C uses static typing, MATLAB Coder must determine the properties of all variables in the MATLAB files at compile time. Therefore, you must specify the properties of all function inputs at the same time as you compile the file.
2. Generate C Code
You then view the generated C code using the code generation report and compare the generated C code with the original MATLAB code.
(1). Click the 'Build' tab, from the Output type drop-down list, select C/C++ Static Library.
(2) check the box 'Generate code only'
(3) Click 'Build' button to generate C code. The Build progress dialog box is displayed. MATLAB Coder generates a standalone C static library <matlabprojectroot>\codegen\lib\<matlabprojectname>, where matlabprojectroot is the folder that contains the m files, and provides a link to the code generation report.
(3) To view the code generation report, click View report.
(4) In the report, click the C code tab to display the list of generated C files.
(5) To view the generated C code, click the link to the target source file, for example,
3. Add the generated C codes into the C/C++ project in Eclipse.
(1) Create a new C project
When creating a new project in Eclipse, the type of the project depends on where are you going to deploy the codes. For example, if you are running the program at the host PC where your Matlab and Eclipse installs, select 'Executable' type and toolchain as 'Cygwin GCC' or other valid C compilers, e.g. 'Custom GCC (MathWorks)'. If the codes will run on a ARM embedded system, select 'ARM Cross Target Application' and a valid toolchain (e.g.,ARM Windows GCC (Sourcery G++ Lite). Note that, in order to have the option of 'ARM Cross Target Application' in the project type list, an ARM plugin is required. See xxx
In this example, we use Cygwin GCC for demonstration. Type a project name, e.g., 'MatlabCoder_1b_GCC', Select Executable > Hellow World ANSIC Project' in the left panel and select 'Cygwin GCC' in the right panel. Click next and finish.
(2) Create a Link folder in the project
The Link folder links to the folder <matlabprojectroot>\codegen\lib\<matlabprojectname> where the C codes generated by the Matlab Coder are stored. There two ways to add a source folder into the project:
Approach 1: Add the folder of the generated C codes into the Eclipse project's build path. This is done by right-click project -> Properties -> C/C++ General -> Paths and Symbols, select 'Source Location' tab. Click 'Link Folder' to add the source files into the Eclipse project.
Approach 2: Another way is to go through File > New > Folder. Click the Advanced section and click "Link to folder in the file system." This creates a virtual folder within your Eclipse project to whatever path you specify. Use this to make the C codes generated by Matlab into a virtual subdirectory of your Eclipse project.
Once set add the source folders into the build path, It is worth setting file 'filter' to add only the *.c/*.h files into the project by excluding other files, like *.bat, *.prj
4. Build the project
(1) Select Project > Build All. Then you will get the executable file projName.exe.
(2) Select Run > Run as > Local C/C++ Applications, you will see the output at the Console as follows:
Note: If you get error: target pattern contains no `%' STOP This is due to Cygwin's new make.exe (3.82) does not recognise the windows path separated by '\'. Rollback to make 3.80 (Geant4) by following commands in Cygwin command window:
You may also need to install libintl2 package from cygwin setup for the required cygintl-2.dll
5. A bit more
Go back to Malab, change the m codes in Matlab (say change the mean of random number to 0), build the Matlab Coder Project. Then switch back to Eclipse, simply build and run, you will find the difference.
Created
with Microsoft OneNote 2010
One place for all your notes and
information