필터 지우기
필터 지우기

Interact with an external c++ application in MATLAB

조회 수: 4 (최근 30일)
Shijie
Shijie 2023년 6월 29일
댓글: Shijie 2023년 7월 5일
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!

채택된 답변

Subhajyoti Halder
Subhajyoti Halder 2023년 7월 5일
편집: Subhajyoti Halder 2023년 7월 5일
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
Shijie
Shijie 2023년 7월 5일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by