Hi Jerome,
From my understanding, you are trying to interface MATLAB with a C library function that requires an input argument of type ‘ _Complex float * ‘. You have encountered an issue when attempting to pass a MATLAB array of complex numbers to this C function using ‘libpointer()’.
The error you're encountering suggests that MATLAB is having trouble interpreting the complex array in the format expected by the C function through the libpointer mechanism, especially since MATLAB's complex number representation doesn't directly map to C's ‘_Complex float’ type.
Given the constraints and the error message, it appears that there might be a misalignment in how MATLAB handles complex data when interacting with C libraries. MATLAB represents complex numbers in a format that is not directly compatible with C's ‘_Complex‘ type, which expects the real and imaginary parts interleaved in memory.
Your second approach, where you manually interleave the real and imaginary parts, is on the right track. However, the issue might be with how you're creating the libpointer. When you interleave the real and imaginary parts, the data is no longer recognized by MATLAB as complex. Instead, it's seen as a real array twice the length of the original complex array, with alternating real and imaginary parts.
Here's how you can adjust your approach:
- Ensure Correct Data Interleaving: You've already interleaved the data correctly, but ensure it's in a single contiguous array suitable for passing to the C function.
- Correctly Define the libpointer Type: Since you've now effectively got an array of float (or single in MATLAB terms) rather than a complex array, you should define your libpointer with a type that reflects this.
Here's a refined version of your code:
array = zeros(1, 10, 'single');
complex_array = complex(array + single(1.0), array + single(1.0));
interleavedArray = zeros(1, numel(complex_array)*2, 'single');
interleavedArray(1:2:end) = real(complex_array);
interleavedArray(2:2:end) = imag(complex_array);
dataptr = libpointer('singlePtr', interleavedArray);
calllib('lib','cmplx_mean_removal', dataptr, 10);
'singlePtr' is used as the type for ‘libpointer’ since you're passing a single-precision, floating-point array (interleaved real and imaginary parts) to the C function. This is assuming your C function is indeed expecting an array of float representing the complex numbers in interleaved format.
‘interleavedArray’ is prepared to exactly match the memory layout expected by the C function for ‘_Complex float * ‘.
For additional information, please refer to the link below:
I hope this helps!
Sameer