converting vector<vector<double>> to mwArray

Hello,
I am trying to pass a 2 dimensional vector (a matrix) in C++ to a C++ share library matlab function compiled by Matlab Compiler.
I am trying to convert this vector, for example myvect, to the mwArray type so that by matlab function can use it. The myvect has the following format in C++,
vector<vector<double>> myvect(2);
myvect[0].push_back(0.1);
myvect[0].push_back(0.2);
myvect[0].push_back(10);
myvect[0].push_back(20);
I am trying:
mwArray mymtrx(2, 2, mxDOUBLE_CLASS);
mymtrx.SetData(&mymtrx[0][0], 4);
But it does not work. The data in mymtrx is not the same as the data in myvect.
How can I convert myvectinto mwArray type?
Thank you

回答 (1 件)

Kaustubha Govind
Kaustubha Govind 2011 年 2 月 22 日

0 投票

Did you have a typo calling SetData?
mwArray mymtrx(2, 2, mxDOUBLE_CLASS);
mymtrx.SetData(&myvect[0][0], 4);

4 件のコメント

hnde
hnde 2011 年 2 月 23 日
Hello,
Sorry for the typo. I am doing:
mwArray mymtrx(2, 2, mxDOUBLE_CLASS);
mymtrx.SetData(&myvect[0][0], 4);
But no matter what data is in myvect, the result that gets printed on the command screen is this :
1.0e+303*
0.0000 -0.0000
0.0000 -2.6570
Is there anything else I can try?
Thank you very much.
Kaustubha Govind
Kaustubha Govind 2011 年 2 月 23 日
I know that something like:
vector<double> myvect(2);
mymtrx.SetData(&myvect[0], 2);
works, because the address of the first element of a std::vector returns the address to the builtin-array represented internally. However, since you are using a vector of vectors, I'm not sure that there is the same guarantee. You might want to explore this kind of conversion for std::vectors before you try this. I'm pretty sure that the problem lies in using &myvect[0][0].
hnde
hnde 2011 年 2 月 23 日
Thank you for your answer.
It is exactly as you said. It works for the vector<double> case, but not for the vector<vector<double>> case.
I am out of ideas on how to do this.
Thank you for all your help.
Jonathan Ingber Katz
Jonathan Ingber Katz 2020 年 11 月 15 日
As Kaustubha Govind said, this in the general case will not work for a vector of vectors. I believe the best way to do this is to recast your vector of vectors as a single vector.

サインインしてコメントする。

カテゴリ

質問済み:

2011 年 2 月 22 日

コメント済み:

2020 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by