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
回答 (1 件)
James Tursa
2017 年 7 月 19 日
編集済み: James Tursa
2017 年 7 月 19 日
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 件のコメント
Thibault Piana
2017 年 7 月 19 日
編集済み: Thibault Piana
2017 年 7 月 19 日
James Tursa
2017 年 7 月 19 日
編集済み: James Tursa
2017 年 7 月 19 日
"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?
カテゴリ
ヘルプ センター および File Exchange で Bartlett についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!