Unrecognized function or variable 'mexUnwrap'.
    6 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaires
  Steven Lord
    
      
 le 29 Juil 2024
				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
  Walter Roberson
      
      
 le 29 Juil 2024
				Perhaps @Pierre is using Apple Silicon and so needs to mex the code in order to generate a mexmaca64 file...
Réponses (1)
  Harsh
      
 le 29 Juil 2024
        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! 
0 commentaires
Voir également
Catégories
				En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



