How to call MATLAB code from C?
The Mathworks site has full details; a demo video of calling the Matlab engine from C, and also the Matlab to C Compiler.
Trouble calling C++ code from MATLAB 2016 using a helper C file w/MEX Wrapper
You cannot put
std::vector<>
in anextern "C"
section, as it's not valid C syntax. You need to create a pure C function stub, compiled as C++, that calls the C++ function.But, you don't need to do any of that because you can compile a C++ MEX-file just fine. The API is C, but you can call C functions from C++ without problem.
The actual problem in your
mexFunction
is that you pass pointers tomxArray
objects where the function expects references tostd::vector<>
etc. The only way to do this right is to copy the data from the MATLAB array into a C++ vector:
#include "myFunc.h"
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs < 5) {
mexErrMsgTxt("Not enough input arguments! 5 expected.");
}
// arg 1: std::vector<myStruct>
if (!mxIsDouble(prhs[0])) {
mexErrMsgTxt("First input argument must be a double array.");
}
std::size_t N = mxGetNumberOfElements(prhs[0]);
double* pr = mxGetPr(prhs[0]);
std::vector<myStruct> a(N);
std::copy(pr, pr+N, a.begin());
// arg 2: std::vector<myStruct>
std::vector<myStruct> b;
// ...
// arg 3: double
double c = mxGetScalar(prhs[2]);
// arg 4: std::vector<std::pair> // This makes no sense in C++, std::pair<> is a template, 2 template arguments missing!!
std::vector<std::pair> d;
// ...
// arg 5: mxArray ** // is this the output? Why does your C++ function take an mxArray?
myFunc(a, b, c, d, &prhs[4]);
}
call c function from matlab
Here is a quick example to get you started:
addOne.cpp
#include "mex.h"
double addOne(double a)
{
return a+1;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs!=1 || nlhs>1) mexErrMsgIdAndTxt("mex:error", "Wrong num of args");
if (!mxIsDouble(prhs[0])) mexErrMsgIdAndTxt("mex:error", "Not double");
plhs[0] = mxDuplicateArray(prhs[0]);
double *x = mxGetPr(plhs[0]);
size_t len = mxGetNumberOfElements(plhs[0]);
for (size_t i=0; i<len; ++i) {
x[i] = addOne(x[i]);
}
}
MATLAB:
>> mex -largeArrayDims addOne.cpp
>> x = magic(4)
x =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> addOne(x)
ans =
17 3 4 14
6 12 11 9
10 8 7 13
5 15 16 2
How can we pass a structure from C code to Matlab code
You have a couple options, depending on your situation.
1) If your C code is structured in such a way that it could be called from matlab, you can compile your C code as a mex file. You would then call your C function from matlab and return your result. To return your structure you have to create a matlab array in your C code and copy the structure parameters into it. The interface for this is the mxArray. It isn't the most convenient process ever but it works well.
mex files and mxArray: http://www.mathworks.com/help/techdoc/matlab_external/f29502.html
2) Use the matlab engine to load your values into matlab without having to compile your code into a mex. The engine will let you execute matlab commands from your C program. You can also load variables into the workspace. You will still have to create an mxArray containing the values you want to copy. You can then use the engPutVariable function to copy the mxArray into your matlab workspace.
Note that the engine maintains its own workspace, it will not automatically copy the variable into the workspace of your current matlab session by default. There are commands to move variables between workspaces but I have not investigated this. I have found it faster to just save the variables to a mat file and load it in the main workspace, but this is not ideal if you need an automated approach.
matlab engine: http://www.mathworks.com/help/techdoc/matlab_external/f29148.html
Related Topics
Detect When Network Cable Unplugged
How to Initialize a Struct to 0 in C++
Is It Illegal to Invoke a Std::Function<Void(Args...)> Under the Standard
How to Output Array of Doubles to Hard Drive
Converting Steady_Clock::Time_Point to Time_T
Calling Erase with Iterator VS Const_Iterator
Trying to Pass String Literals as Template Arguments
Explicit Call to Destructor Is Not Destroying My Object Why
Should Std::Unique_Ptr<Void> Be Permitted
How to Convert a Value from Host Byte Order to Little Endian
Changing C++ Output Without Changing the Main() Function
Opencv Cv::Mat to Std::Ifstream for Base64 Encoding
Convert Std::String to Qstring
Equivalent of Console.Readline() in C++
What Rules Does Compiler Have to Follow When Dealing with Volatile Memory Locations
Stl Ordering - Strict Weak Ordering
Boost::Spirit How to Parse and Call C++ Function-Like Expressions