Interact with an external c++ application in MATLAB

3 Ansichten (letzte 30 Tage)
Shijie
Shijie am 29 Jun. 2023
Kommentiert: Shijie am 5 Jul. 2023
I have a MATLAB script that
  1. Generates a large dataset and saves the dataset to the disk,
  2. Calls an external C++ application using "system(command)" to process the dataset,
  3. And imports the output generated by the external application from disk.
The bottleneck in this approach is the data transfer time needed to save/load the dataset to/from the disk. Therefore I am thinking about skipping these steps and import/export that data directly into/from the C++ application. I wonder what the easiest way is to achieve this. My understanding is that I need to create a MEX function as the interface to my C++ application. Can someone please help? Thanks!

Akzeptierte Antwort

Subhajyoti Halder
Subhajyoti Halder am 5 Jul. 2023
Bearbeitet: Subhajyoti Halder am 5 Jul. 2023
Hi Shijie,
It is my understanding that you want to use an external C++ application through MEX files and get it’s output directly to your MATLAB script.
You need to perform the following steps to use an external C++ application through MEX files:
  1. Create your C++ function.
  2. Implement it in the MEX compatible C++ source file.
  3. Compile the C++ code with MEX command in MATLAB.
  4. Call the C++ function from MATLAB.
The C++ source file typically has the following code flow:
  1. Check for input and output argument counts.
  2. Convert the input arguments (if any) from MATLAB datatypes into C++ datatypes, using helper functions in “mex.h”.
  3. Invoke your C++ function with these converted arguments.
  4. Save the function output (if any) and convert them back to MATLAB dataypes.
  5. Store them as output arguments of the MEX file.
For your reference, here is a sample C++ source file (myfunction.cpp) to add 2 numbers and return the output
#include "mex.h"
void myfunction(int a, int b, int* result) {
*result = a + b;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int a, b, result;
// Check the number of input arguments
if (nrhs != 2) {
mexErrMsgIdAndTxt("myfunction:nrhs", "Two input arguments required.");
}
// Check the number of output arguments
if (nlhs > 1) {
mexErrMsgIdAndTxt("myfunction:nlhs", "Too many output arguments.");
}
// Get the input arguments
a = (int) mxGetScalar(prhs[0]);
b = (int) mxGetScalar(prhs[1]);
// Call the C function
myfunction(a, b, &result);
// Create the output argument
plhs[0] = mxCreateDoubleScalar((double) result);
}
Now, compile the C++ source code:
mex myfunction.cpp
Call the C++ function from MATLAB:
a = 10;
b = 25;
res = myfunction(a,b);
Kindly note to handle the datatype conversions carefully to avoid segmentation faults.
For more details on ‘MEX’, kindly go through the following documentations,
  1 Kommentar
Shijie
Shijie am 5 Jul. 2023
Hi Subhajyoti,
Thank you for your detailed response! I was also wondering if there is any alternative approach to enable a MATLAB script to interact with an external c++ application.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by