Mex & shared library

13 Ansichten (letzte 30 Tage)
jason beckell
jason beckell am 23 Jan. 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.

Antworten (4)

Titus Edelhofer
Titus Edelhofer am 23 Jan. 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 am 23 Jan. 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 am 23 Jan. 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 Kommentar
Kaustubha Govind
Kaustubha Govind am 23 Jan. 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.

Melden Sie sich an, um zu kommentieren.


jason beckell
jason beckell am 23 Jan. 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 Kommentar
Walter Roberson
Walter Roberson am 23 Jan. 2012
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by