Re: Using MATLAB functions in C code



On Sun, 17 Feb 2008 18:44:02 +0000 (UTC), "dfgdfg " <AP@xxxxxxxxxxxxx>
wrote:

I'm writing a C code and I want to use fft MATLAB function.I
looked through documentation and found that
engOpen,engPutVariable and similar functions provide
interface to MATLAB engine(functions).When I tried to
compile C file wiht (mex code.c) command and error:
"undefined reference to _egOpenEngine" occured.In
documentation it is said that an lcc compiler with .bat
option files needed.The command is as follows: (lcc -f
*engoptions.bat code.c).I typed this command and everithyng
worked fine.The problem is that lcc compiler produces an
executable file.But i don't need it.I want to execute my
code from MATLAB command line like : y=Cfunc(a).
Cfunc must be written in C code and must be able to use
MATLAB fft amd ifft functions.I just want to compile them like :
mex code.c.

A simple wrapper outline to get you started. This assumes that you are
not passing anything to/from the MATLAB workspace.

James Tursa

-----------------------------------

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])
{
int mlhs, mrhs;
mxArray *lhs[1], *rhs[3];

// Insert your C code here

// Convert your C variables to mxArray(s) rhs[0], rhs[1], rhs[2]
here

mrhs = 3;
mlhs = 1;
if( mexCallMATLAB( mlhs, lhs, mrhs, rhs, "fft") )
mexErrMsgTxt("Call to fft didn't work\n");
mxDestroyArray(rhs[0]);
mxDestroyArray(rhs[1]); // If needed
mxDestroyArray(rhs[2]); // If needed

// Convert the result lhs[0] back into a C variable

// Use your resulting C variable here

mxDestroyArray(lhs[0]); // When done with lhs[0], destory it

// Convert your C variables to mxArray(s) rhs[0], rhs[1], rhs[2]
here

mrhs = 3;
mlhs = 1;
if( mexCallMATLAB( mlhs, lhs, mrhs, rhs, "ifft") )
mexErrMsgTxt("Call to ifft didn't work\n");
mxDestroyArray(rhs[0]);
mxDestroyArray(rhs[1]); // If needed
mxDestroyArray(rhs[2]); // If needed

// Convert the result lhs[0] back into a C variable

// Use your resulting C variable here

mxDestroyArray(lhs[0]); // When done with lhs[0], destory it
}
.



Relevant Pages

  • Re: Using MATLAB functions in C code
    ... interface to MATLAB engine.When I tried to ... option files needed.The command is as follows: ... MATLAB fft amd ifft functions.I just want to compile them ... what about using mex wrapper on your C file, ...
    (comp.soft-sys.matlab)
  • Re: Linking C++ and Matlab using VS2003
    ... command from the MATLAB command line, ... do the compile from within the Visual Studio environment? ... I usually do the compile using the mex command at the ... MATLAB prompt, but this usually means I have to manually ...
    (comp.soft-sys.matlab)
  • Re: Need help for matlab2fmex
    ... example and matlab told me: ifort: command not found. ... best use the -n option in the matlab command line for the mex compilation. ... but only dump the commands which will be used to compile your file. ...
    (comp.soft-sys.matlab)
  • Re: strange slow down
    ... I compiled my matlab program on a 64-bit Linux cluster (10 ... processes running the same command, ... compile). ...
    (comp.soft-sys.matlab)
  • Re: Matlab -- Venting, Plus Miscellaneous Questions
    ... There are no command lines. ... Matlab is obviously intensively command-driven -- which means, ... print or online documentation. ... calculations, but symbolic calculations keep showing some ridiculous ...
    (sci.math.symbolic)