Mex & shared library

18 visualizzazioni (ultimi 30 giorni)
jason beckell
jason beckell il 23 Gen 2012
Hello to everyone,
I'm a student and I'm not very expert in Matlab. I am writing a mex file (double.c) under Linux which should call a routine from a shared library (lidouble.so). How can I do that? Which command should I write to fulfill my purpose?
Thanks to everyone for your kind attention and my best regards,
Jason.

Risposte (4)

Titus Edelhofer
Titus Edelhofer il 23 Gen 2012
Hi,
usually it is sufficient to do the following: add an include statement for the library to your mex file, so that the compiler "knows" the functions. Second, when compiling your code with mex add the library using the switch "-l" (see doc).
Titus

Kaustubha Govind
Kaustubha Govind il 23 Gen 2012
This is what you are doing:
y = mxGetPr(plhs[0]);
y = timestwo(x);
The function timestwo overwrites the input pointer 'x' with two times its value and returns it - effectively overwriting the pointer 'y' with the location of 'x'. Therefore the original memory that was allocated for the output remains untouched:
plhs[0] = mxCreateDoubleMatrix(mrows, ncols, mxREAL);
Try making it:
*y = *(timestwo(x));
So that the values are copied instead of the pointer getting overwritten. Does this fix the issue?

jason beckell
jason beckell il 23 Gen 2012
Hello Kaustubha,
thank you very much for your precious help, now I got it clear and your solution definitely works! :)
Thank you very much again!
Jason.
  1 Commento
Kaustubha Govind
Kaustubha Govind il 23 Gen 2012
Glad to be of help. Also, please note that my solution works only as long as the output is a scalar. In case of an array/matrix, you'll have to loop over the value returned by timestwo and copy it into y.

Accedi per commentare.


jason beckell
jason beckell il 23 Gen 2012
Hello Titus,
thank you very much, now it finally works, thank you again! Anyway, now I have a further little question to ask you. Just to produce an example, I wrote down the following mex file (test.c)
#include "mex.h" #include "double.h" #include "matrix.h"
void myExitFcn() { mexPrintf("MEX-file is being unloaded"); }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *x, *y; int mrows, ncols;
/* Check for proper number of arguments. Just one input and one output*/
if (nrhs != 1) {
mexErrMsgTxt("One input required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!(mrows == 1 && ncols == 1)) {
mexErrMsgTxt("Input must be a noncomplex scalar double.");
}
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
plhs[0] = mxCreateDoubleMatrix(mrows, ncols, mxREAL);
y = mxGetPr(plhs[0]);
/*Print the value of x */
mexPrintf("Value x: %f \n\n", *x);
/*call the routine timestwo which is listed in double.h */
y=timestwo(x);
/*Print the value of y */
mexPrintf("Value y: %f \n\n", *y);
if(mexAtExit(myExitFcn))
{
mexPrintf("Error unloading function!");
}
}
Let this be the file double.h
double* timestwo(double* x);
and let this be file double.c
#include math.h #include stdio.h
double* timestwo(double* x) { (x) = 2.0 (*x); return x; }
Let a=8, if I compile it and then I type:
b = test(a)
I get the following outputs
Value x: 3.000000
Value y: 6.000000
c =
0
How come c = 0? Shouldn't it be equals to 6.000000? Thank you!
  1 Commento
Walter Roberson
Walter Roberson il 23 Gen 2012
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Accedi per commentare.

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by