Pass complex number to shared library

Hi !
I've succefully passed matrix, string, double, integer and other data format from Matlab to my C shared library (and reciprocally).
But now, I've to pass complex number from matlab to my library and return complex number from this library to Matlab in other function.
But, after many attempts, I don't know how to do that :(
Is a simple method exist to do that ?
Ty

Antworten (1)

James Tursa
James Tursa am 19 Jul. 2017
Bearbeitet: James Tursa am 19 Jul. 2017

1 Stimme

I am not familiar with the use of libpointer, so I am just guessing here, but maybe for complex numbers you need to arrange the real & complex parts interleaved in memory first since that is how the C/C++ routines actually use them. E.g.,
z = 2 + 3i; % <-- the real and imag data are not next to each other in memory
c = [real(z);imag(z)]; % <-- Use this version to pass
If you are passing an array, then maybe
c = [real(z(:))';imag(z(:))'];

2 Kommentare

Thibault Piana
Thibault Piana am 19 Jul. 2017
Bearbeitet: Thibault Piana am 19 Jul. 2017
Hi ! Thanks for your answer
I've tried something like this, it works for the Matlab -> C way, but it's not very practical. Moreover, on the other way (C -> Matlab), It's more complicated :/ At the moment, the only way to do it for me, it's to return an array allocated with a malloc, and it's not really efficient... :(
In addition, I want to have the possibility to access and modify complex matrix directly in C. I've succefully do it with double matrix, but it doesn't work for complex matrix :'(.
James Tursa
James Tursa am 19 Jul. 2017
Bearbeitet: James Tursa am 19 Jul. 2017
"I've tried something like this, it works for the Matlab to C way, but it's not very practical."
The C/C++ routines need the real & imag data interleaved since that is how complex data is represented in C/C++. Given that, I don't see how you can avoid the data copy steps in both directions. So, practical or not, I don't see how you can get around this unless you rewrite your C/C++ library to not use complex numbers.
"Moreover, on the other way (C to Matlab), It's more complicated"
Why so? If you get back a linear array of complex number value pairs from the C/C++ code, simply split it apart. Again, I don't see how you can avoid this data copy unless you rewrite your C/C++ library code:
C = result from your library call
Z = C(1:2:end) + C(2:2:end)*1i; % the complex equivalent on MATLAB side
"In addition, I want to have the possibility to access and modify complex matrix directly in C. I've succefully do it with double matrix, but it doesn't work for complex matrix"
I don't see the reason why you can't do just that, or maybe I don't understand what exactly you are trying to do. Once you make the complex data contiguous and interleaved, and pass that to your library routine, why can't your routine work with that? And why can't you get the result back into MATLAB? For the C/C++ routine arguments that are complex, just create a different signature in the header file that pretends these are simply double. What do these function signatures look like?

Melden Sie sich an, um zu kommentieren.

Produkte

Gefragt:

am 19 Jul. 2017

Bearbeitet:

am 19 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by