Unrecognized function or variable 'mexUnwrap'.

2 次查看(过去 30 天)
I get this error: “Unrecognized function or variable 'mexUnwrap'” when I run my code. However, the folder containing this C++ file (mexUnwrap.c) is in my path. It also contains mexUnwrap.mexa64, mexUnwrap.mexmaci64, mexUnwrap.mexw64. Do you have any suggestions for solving this problem?
  2 个评论
Steven Lord
Steven Lord 2024-7-29
What does this command show when you run it on your machine? Do you have a file named mexUnwrap with that extension in the folder?
mexext
mexa64
If not, you'd need to build that MEX-file on that platform.
Walter Roberson
Walter Roberson 2024-7-29
Perhaps @Pierre is using Apple Silicon and so needs to mex the code in order to generate a mexmaca64 file...

请先登录,再进行评论。

回答(1 个)

Harsh
Harsh 2024-7-29
Hi Pierre,
From what I can gather, you are unable to use the “mexUnwrap” function in MATLAB. To use functions or variables from C/C++ in MATLAB, please follow these steps:
1. Create your C/C++ file in the following format, ensuring you include “mex.h”:
#include "mex.h"
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
/* variable declarations */
double num1, num2, result;
/* check for proper number of arguments */
if(nrhs != 2) {
mexErrMsgIdAndTxt("MATLAB:sampleFunction:invalidNumInputs",
"Two inputs required.");
}
if(nlhs != 1) {
mexErrMsgIdAndTxt("MATLAB:sampleFunction:invalidNumOutputs",
"One output required.");
}
/* ensure the inputs are scalar */
if(!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ||
mxGetNumberOfElements(prhs[0]) != 1 ||
mxGetNumberOfElements(prhs[1]) != 1) {
mexErrMsgIdAndTxt("MATLAB:sampleFunction:inputNotScalar",
"Both inputs must be scalar.");
}
/* get the value of the scalar inputs */
num1 = mxGetScalar(prhs[0]);
num2 = mxGetScalar(prhs[1]);
/* perform the addition */
result = num1 + num2;
/* set the output pointer to the output matrix */
plhs[0] = mxCreateDoubleScalar(result);
}
2. Compile the "mex" function using the following command:
mex sampleFunction.c
Once compiled, you can start calling “sampleFunction” in MATLAB.
I hope this helps, thanks!

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by