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.

 

Machine generated alternative text: 4k Build
¡ Build Finished.
A Hide Details [ Close ]
» coder -build MatlabCoder_lb.prj
Code generation successful: View report
Save to log file

 

(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.

 

Machine generated alternative text: Project name: genRnd_GCC
L?1 Use default location
Location: E:\Beagle\Eclipseworkspace\genRnd_GCC Browse...
Choose file system: [default .rj
Project type: Toolchains:
ARM Cross Target Application s Cross 6CC
• Empty Project Custom 6CC (MathWorks)
. ARM Cross Target Static Library Cygwin 6CC
Executable Microsoft Visual C++
• Empty Project
• Cross-Compile Project
• Hello World ANSI C Project _____________________________
RJ Show project types and toolchains only if they are supported on the platform
<Back Next> Finish Cancel j

 

(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.

Machine generated alternative text: J Properties for coderand2 L 2 j’
type filter text Paths and Symbols w w V
Resource
Linked Resources
Resource Filters Configuration: Debug [Active] lE [Manage Configurations...]
Builders
C/C++ Build
C/C++ General [Includes [ # Symbols a Libraries [Library Paths š eLocationi References
Code Analysis Source folders on build path: [Add Folder...]
Code Style ___________
j [ /coderand2 [Link Folder...]
Documentation I I
I I  /coderand2/Frommatlabcoder ____________
File Types I I I 
Indexer J New Folder ___________
Language Mappings [ Delete ]
Paths and Symbols Folder name: Frommatlabcode
Project References
Run/Debug Settings [__«Advanced__[
Task Repository Ru Link to folder in the file system
WikiText
E:\Beagle\Test\MatlabCoder\helloworl d\codegen\exe\coderand [ Browse... ] [ Variables... [
Choose file system: [default w]
Location E:\Beagle\Test\MatlabCoder\helloworld\cod...r resource. This can cause unexpected side-effects.
OK Cancel [
1 LRestore Defayj [ Apply ]
LE OK JE] j Cancel [

 

 

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.

Machine generated alternative text: 3 New Folder
Folder
Create a new folder resource. V i
Enter or select the parent folder
MatlabCoder_lb_GCC/src
T MatlabCoderjb_GCC
L’ Debug
src
¡a RemoteSystemsTempFiles
Folder name: matlabcode
[__«Advanced__1
Q  Use default location
! D  Folder is not located in the file system (Virtual Folder)
Ô  Link to alternate location (Linked Folder)
E:\Beagle\Eclipseworkspace\MatlabCoder_lb\codege L Browse... Variables...
Choose file system: Ldefault
[Resource Filters... j
[ Finish J r Cancel j

 

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:

 

Machine generated alternative text: Problems .i Tasks  Console z’\ Properties] 
<terminated> MatlabCoder.1b_GCC.exe [C/C÷ + Applical
!1!Hello World!!!
getRand=11. 075334
getRand=13 .667770
getRand=5 .482306
getRand11 .724347
getRand=10.637530
getRand=7. 384623
getRand=9.132816
getRand=10 .685249
getRand=17 .156794
getRand=15. 538874
4

 

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:

  • cd /usr/bin
  • mv make.exe make_381.exe
  • chmod +x make.exe

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