简体   繁体   中英

How to loadlibrary in MATLAB with complex data?

I have created two shared libraries in C. One of them, called Alib, has functions that has double complex * arguments. Another one, called Blib, uses the first one and returns results of type double . The goal is to use functions of Blib in MATLAB with using loadlibrary command. However, I need to load both libraries, but when library Alib is loaded, I get this error:

Type 'intdouble_ComplexPtr' was not found.  
Defaulting to type voidPtr.

It seems that MATLAB doesn't recognize type double complex * . The final result is that MATLAB crashes when calllib function is called. I have tested my library with C code, and everything works. How can I use loadlibrary with complex data? Thanks!

It depends on the compiler you are using. C99 implements complex numbers different in comparison to c++. All complex function have defined in complex.h. My suggestion is that don't engage your self with complex numbers, instead separate imaginary part and real part in another function, then compile your code and at the end, use that function in MATLAB. For example if you have a function like

double complex f(double complex x)
{
...
}

Then define another function like:

void fmatlab(double xreal, double ximag, double* yreal , double* yimag)
{
double complex temp=f(complex(xreal,ximag)); // complex is supposed to define a complex number, depends on implementation of complex numbers.
*yreal=real(temp);
*yimag=image(temp); // depends on implementation.
}

Finally use the last function in MATLAB.

MATLAB stores complex numbers differently than Fortran. MATLAB stores the real and imaginary parts of a complex number in separate, equal length vectors, pr and pi. Fortran stores the same complex number in one location with the real and imaginary parts interleaved.

As a result, complex variables exchanged between MATLAB and a Fortran function are incompatible. Use the conversion routines, mat2fort and fort2mat, that change the storage format of complex numbers to address this incompatibility.

As pointed out but user3528438 C stores complex number as Fortoran and thus is incompatible too.

source: http://www.mathworks.com/help/pdf_doc/matlab/apiext.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM