Hey everyone,
Here's what I am trying to do. I have found some great source code online for a free simulation software written in C++. While the software is great, I'd like to integrate it into MATLAB. I understand that using MEX functionality in MATLAB you can use C++ source code in MATLAB. I'm not too familiar with this, so a tutorial or links to help me out would be great. I can provide a link to the source code if needed? But I'm really looking for a general implementation.
I read through some documentation but as one can expect the learning curve is significant.

3 commentaires

James Tursa
James Tursa le 3 Mai 2017
Is this simulation interactive, or will it run essentially as a batch job once given some inputs? What is the output of the simulation? Some variables or plots or files or ...?
Walter Roberson
Walter Roberson le 3 Mai 2017
If you can create static class methods with extern "C" to interface to the simulation functions, then you can probably call the routines by using loadlibrary()
Note: loadlibrary() does not work to call general C++ methods, only things that extern "C"
Hanif
Hanif le 4 Mai 2017
The output of the simulation generates binary files that need further analysis. The inputs into the simulation are saved as a binary function - the code searches for a specific filename or uses default simulation parameters.

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 4 Mai 2017

1 vote

Integrating some C++ code in a mex file is not hard: The mex file is started at the entry function "mexFunction". In tis function 3 things happen:
  1. Input arguments comming from Matlab are converted to C++ variables,
  2. The C++ functions are called,
  3. The output is converted back to Matlab variables.
For the 1st job, e.g. mxGetPr() gets the pointer to the data of a Matlab double array. mxGetM /mxGetN gets the dimensions of matrices, etc. Check the types of the inputs by mxIsDouble and reject unexpected types, otherwise the C++ part will crash.
The 3rd job uses e.g. mxCreateDoubleMatrix(). This creates a matrix without setting the values. Either copy the results from the C++ part using memcpy, or create the output arrays initially and provide a pointer to the reserved memory directly to the C++ part.
You see, the mexFunction is the gateway between Matlab and C++ only.

3 commentaires

Hanif
Hanif le 4 Mai 2017
The MEX file seems to be the key to the solution. I read: https://www.mathworks.com/help/matlab/matlab_external/standalone-example.html
Please clarify the following:
- Lets say I have a C function that initiates my simulation, but calls other C files for different parts of the simulation. Do I need a MEX file for each of the different C files or just one for my main? In other words - Once the C code that initialises and starts the simulation is called, will it automatically call each C file and run or does each C file used for the simulation need to be included in a MEX file?
James Tursa
James Tursa le 4 Mai 2017
Include all of your source files in one mex command so they will all get linked together into one mex routine.
Hanif
Hanif le 4 Mai 2017
Such as:
#include "File1.h"
#include "File2.h"
#include "File3.h"
etc..

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by