How to interact with matlab function (complex array as argument) from Qt (C++)
Show older comments
I have a matlab function which takes a multi-dimension complex array as argument, and which return a multi-dimension complex array. I'm like to call that in my Qt application.
I've done some study and I understand I can compile my matlab function and call it from Qt. But how can I pass multi-dimension complex array back and forth? In Qt I can do QList<std::complex<double>> as the data type but is that what I can pass to Matlab function? Thanks.
10 Comments
Walter Roberson
on 16 Aug 2018
Edited: James Tursa
on 16 Aug 2018
The method depends upon which MATLAB release you are using. The handling of complex arrays changed as of R2018a.
Peng Ye
on 16 Aug 2018
James Tursa
on 16 Aug 2018
Edited: James Tursa
on 16 Aug 2018
R2017b and earlier: Real & Imag data are stored in separate memory blocks.
R2018a and later: Real & Imag data are stored interleaved in a single memory block.
Since you are using R2015b, your real & imag data are stored in two separate memory blocks. This is a MATLAB thing that typically does not match up with very many (any?) other programming languages which store real & imag data in an interleaved fashion (like R2018a). You will probably need to copy the separate real & imag data to a new memory block in an interleaved fashion in order to match it up with the C++ std::complex<double> type.
Walter Roberson
on 16 Aug 2018
Edited: Walter Roberson
on 16 Aug 2018
https://en.cppreference.com/w/cpp/numeric/complex std::complex<double> uses the interleaved format that is new in R2018a, so the interface would perhaps be less work with newer versions.
James discusses this more at https://www.mathworks.com/matlabcentral/answers/390105-r2018a-complex-interleaved-data-is-the-new-memory-model
Peng Ye
on 17 Aug 2018
James Tursa
on 17 Aug 2018
Edited: James Tursa
on 17 Aug 2018
What is your Qt code going to do with this data? If you are using R2015b then MATLAB natively stores the real and imag parts separated (discussed above). As long as your Qt code can deal with that OK then this approach avoids a data copy (preferred). But if your Qt code needs to call some complex functions that expect interleaved data, then you will be forced to do a copy-in-copy-out of your data.
Peng Ye
on 17 Aug 2018
Peng Ye
on 18 Aug 2018
Walter Roberson
on 18 Aug 2018
That looks like it should be fairly efficient.
In MATLAB, the equivalent would be
C = complex(A, B);
Peng Ye
on 18 Aug 2018
Answers (0)
Categories
Find more on Deploy to C++ Applications Using mwArray API (C++03) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!