This document discusses how to convert MATLAB functions to C/C++ code and vice versa using MATLAB Coder and MEX files. It presents the steps to take a MATLAB function, generate C/C++ code using MATLAB Coder, import and build the code in Microsoft Visual Studio, and test the compiled program. It also outlines how to take existing C/C++ code, compile it into a MEX file using the mex compiler in MATLAB, and call the function from MATLAB. The document aims to help interact between MATLAB and C/C++ code.
5. Define Matlab function
% Here we have “x & y & z” as input and “a & b” as
output.
function [a, b]= mymatfun (x,y,z)
a=x+y+z;
b=x*y*z;
end
6. First we generate code with
MATLAB and test it in
Microsoft Visual studio.
I use C++ program because I am
going to debug it via Visual Studio.
I use Microsoft Visual Studio 2015
for C++ compiler.
Matlab Coder
7. Matlab Coder In two way ,you can call coder:
Typing “coder” in Command Window
Using APPS
9. Matlab Coder
Step I : Calling coder
Step II : Define Function
You should define input type:
(single , double , ……)
10. Matlab Coder
Step I : Calling coder
Step II : Define Function
In this level, you can check your code
for any issues:
Step III : Declare input type
11. Matlab Coder
Step I : Calling coder
Step II : Define Function
Here is most IMPORTANT section that
you should set your setting for
generating code.
Step III : Declare input type
Step IV : Check
12. Matlab Coder
Step I : Calling coder
Step II : Define Function
Step III : Declare input type
Step IV : Check
Step V : Generate code option
13. Matlab Coder
Step I : Calling coder
Step II : Define Function
Step III : Declare input type
Step IV : Check
Step V : Generate code option
Step V - I : More setting
The
.cpp & .h
files are ready
Press Generate
Click to see
17. Visual Studio
Step I : Create Empty Win32 app
Here we import the file that the Matlab
generated.
18. Visual Studio
Step I : Create Empty Win32 app
Here we import the file that the Matlab
generated.
Here I write my code in VS 2015.
I use “mymatfun” as function.
19. Visual Studio
Step I : Create Empty Win32 app
Step II : Import .cpp & .h files
Press F5 for debugging.
And here is The FINAL PROGRAM…
20. Let’s look at what we done…
Visual StudioMatlab Coder
22. Mex Introduction
A MEX file is a type of computer file that provides an
interface between MATLAB or Octave and functions
written in C, C++ or Fortran. It stands for "MATLAB
executable".
23. Mex compiler
• First of all you must Install “mex compiler”.
• You can see the supported compiler in
www.mathworks.com
• We use Microsoft Visual studio 2015 that Matlab 2016b
support it.
25. Here is our C code
#include <math.h>
void timestwo (double y[], double x[])
{
y[0] = 2.0*x[0];
return;
}
26. Little change in code!!!!!
For build mex from C program , the code should be
change.
The library of mex is “mex.h” and all of code must
change to “mex.h” order.
27. Type the code in
Matlab editor
and save it as
“.c” text
28. Here is FINAL STEP…
• Enter “ >> mex C/C++ code program name ”
For example:
>> mex mycfun.c