how to accelerate your matlab programme -mex method(1) 2. How to ues Matlab coder 3.How to use your source code or your MEX function. 4 matters needing attention

It is a common sense that the efficiency of Matlab is inferior to the efficienct of C programme,especially for “for cycle promamme”. so the “for cycle matlab progamme” can be replaced by C programme.

As we know, the newest Matlab(2016b) support the Hybrid Programming in Matlab programme with C or C++ programme. Also the Matlab offer a tool named Matlab Coder to transfer your Matlab programme to C programme or C++ programme.

So the C or C++ programme transferring from Matlab Coder replaced Matlab in some key section. These are the basis of the acceleration Matlab programming.

2. How to ues Matlab coder

2.1 Before you strat transferring

There are something you should know before the transferring your Matlab programme.

  • Matlab coder could not transfer the script nor live script only the function.
  • Matlab coder do not support all the function, please prgramming Matlab in C programming thinking.
  • Please transferring the Matlab programming which strictly follow the coding style.
  • Please certain the type the input and output matrix. like column vector,row vector,scalar or matrix.
  • I strongly recommend that all the input,output coefficients and intermediante variables should assigned a specific data type in Matlab programme, like logic,uint8,uint16 single or double.
  • I also strongly recommend that there also should have a script as testbench which call the funcion you want transfer. Please programming this testbench following the testbench standard. And also you can just use the original dscript

2.2 Start your transferring your Matlab programming.

Just use coder in command window, there is the the Matlab coder

###2.2.1 Select
In the first step, you can select the which funcion(.m file) you want transferring and numeric conversion.
If you did the assign a specific data type, just forget numeric conversion.
If your Matlab program is unstable, you can leave all you variables as double type.

2.2.2 Review

In second step, the coder will check availbility of the funcion. For example, the tool will check all the availability in of funcion you call in your funcion for Matlab Coder and the syntax error.

2.2.3 Define

In third step, you must assign every your input and output variables data type. However, if you have a testbench like a upper layer funcion or script(personal recommend: script), the input data type will be define automatically.

2.2.4 Check Run-time Issue

In this step, the Matlab coder will creat a trial C or C++ coder with MEX interface. this trail code is generated for Matlab host operation environment.
And then the tool will check the result of C/C++ with original Matlab programme. And also you will find there is a new codgen subfolder in whcih there are subfolder name mex. All the intermediate files will store in the mex folder.Further, the mex folder is useful.

Also you can find the called funcion stack(if the function you want transfer also call some subfunctions)

By the way, MEX is the interface between Matlab and C/C++/Fortran. All C/C++/Fortran must called through MEX interface. Even you want call a C/C++/Fortran programming, please write a mex interface funcion as a interface.

2.2.5 Generate

Congratulation! Matlab Coder finished all checking in step.

Now let’s get start to transferring. First of Generating, there are some key setting you must take care.

  • Build type:there are 5 different options, “Source code, MEX,Static libarary(.a),Dynamic library(.so),Executable”.

Unfornately I only used the firs two options( Source code & MEX), if you just want accelerate your Matlab programme. please select the MEX. If you also want see your source code(like C/C++), please select the Source code.

If you select the the option, source code, there are extra option you must select. these are showed below.

  • Language
    As I mentioned above, please select C or C++.

  • Hardware Board & Device
    Please assign a running environment for the transferred source code. It is very important to the number of different data type(like 8bit or 16bit). However there are a sweet Matlab Host Computer you can select.

  • ToolChain
    Just a selection to assign a compile tool like GCC or something else to Matlab Coder.

OK, there are all for transferring your Matlab programme to C/C++.

3.How to use your source code or your MEX function.

No, there are Mex function or source code in your folders. you can call it like a mex function just a Matlab progamme.

For example, the original funcion named 1.m, you call it like […]=1(…). Now you can call […]=1_mex(…)()
(if you select the MEX option in 2.2.5 Generate)
1_m is running as C/C++ progamme.

If you select MEX option in in 2.2.5 Generate, Matlab will call Generated MEX funcion in that step. Otherwise Matlab will call the Generated MEX funcion in the 2.2.4 Check Run-time Issue. But I strongly recommened you select the MEX in 2.2.5 Generate step. Anyway the MEX function generated in 2.2.4 Check Run-time Issue is just a trail funcion.

4 matters needing attention

Anyway, the MEX or source code are not fitting everything. In some cases, please do not use this method.

4.1 Date type

As mentioned above, I personnal strongly recommended assigning data type to every input, output and intermediate variables. But, if the funcion is unstable and need to be adjusted, which means you may change your data type or matrix size. Please use the original Matlab programme. Do not waste your time in transferring programme after everytimes adjusting. In my opinion, this method is quite fittin build the running environment, because it is stable.

4.2 tools tips

Anyway, #Matlab coder# is just a tool attached in Matlab. There are some little bugs and inconverience. From my experience using in a Linux distortion, there always are some problem with overwrite a previous Mex function. Also, if you transfer the function more than 1, Ok, sometimes you will meet a unreasonable mistaking.

4.3 Next plan

Except Mex method, I don’t other method to accelerate running of Matlab programme. If I get any ideas, I will renew my blog.