How to interact with matlab function (complex array as argument) from Qt (C++)

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

The method depends upon which MATLAB release you are using. The handling of complex arrays changed as of R2018a.
oh, I'm using 2015b. Is the change some improvement I should take advantage of or is it just different way of doing thing? I try not to use newer release as the runtime library size is larger... Thanks.
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.
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.
I'm thinking maybe to pass two double arrays instead of one complex array, one for the real and one for the imag, then make the complex in my matlab function. Any downside to this? Thanks.
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.
I see. So for R2015b I would have to shuffle the data around (data copy) anyway if I have to use the complex number in Qt. That is a good point so I will try to put all complex operations in matlab function and keep data in two separate arrays in my Qt side where the data may be passed around.
Now for 2018a and after, keeping it two arrays is not as optimized as a complex array? Because I need to do a matrix plus operation in Matlab every time to use the complex array?
Thanks.
I found this somewhere online. So I can basically build the complex array with mwArray. Is this a better idea than passing two arrays into matlab and do C=A+j*B in matlab?
double rdata[4] = {1.0, 2.0, 3.0, 4.0};
double idata[4] = {10.0, 20.0, 30.0, 40.0};
mwArray a(2, 2, mxDOUBLE_CLASS);
a.SetData(rdata, 4);
a.MakeComplex();
a.Imag().SetData(idata, 4);
That looks like it should be fairly efficient.
In MATLAB, the equivalent would be
C = complex(A, B);
Thanks a lot for the help. I have this part sorted out... but I have a big problem preventing me from using these... Can you help taking a look? Thanks.
https://www.mathworks.com/matlabcentral/answers/415314-undefined-reference-error-when-calling-matlab-function-in-qt

Sign in to comment.

Answers (0)

Asked:

on 16 Aug 2018

Commented:

on 18 Aug 2018

Community Treasure Hunt

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

Start Hunting!