Building a mwArray Structure from C++

6 次查看(过去 30 天)
SDS
SDS 2012-7-26
编辑: Co Melissant 2018-11-13
I am having trouble finding working examples, how can I build the same Matlab data structure in C++ as is built in MATLAB script:
function data = GenData()
sub.test1 = 1;
sub.test2 = 2;
data.one = 1;
data.two = 2.0;
data.string = 'string';
data.subtest = sub;
end
I tried using something similar to this code:
const char* data_fieldnames[] = {
"one", "two", "string", "sub"
};
const char* sub_fieldnames[] = {
"test1", "test2"
};
mwArray data(1,1,4,data_fieldnames);
mwArray one(1);
mwArray two(2.0);
mwArray string("string");
mwArray sub(1,1,2,sub_fieldnames);
mwArray test1(1);
mwArray test2(2);
sub.Get(1,1).Set(test1);
sub.Get(1,2).Set(test2);
data.Get(1,1).Set(one);
data.Get(1,2).Set(two);
data.Get(1,3).Set(string);
data.Get(1,4).Set(sub);
mwString output_s = data.ToString();
const char* output_c = (const char*)output_s;
The output string gives me each name(one,two,string,sub) followed by "[ ]". I expect the correct output string would give me the same as a disp(data) would in MATLAB:
one: 1
two 2
string: 'string'
sub: [1x1 struct]
What am I doing wrong/How do I build this structure properly? Any help would be very much appreciated.

回答(2 个)

Co Melissant
Co Melissant 2014-4-17
When creating a structure, the third argument to mwArray is not the number of fields, its the type , so using the syntax like:
mwArray sub(1, 1, mxSTRUCT_CLASS, sub_fieldnames)
results in improved readability AND solves the issue.
  2 个评论
Will Grant
Will Grant 2014-7-22
Are we potentially dealing with API version mismatches?
In R2014a, structs are only created with two forms of the mwArray() constructor, and neither of them directly take an mxClassID as an argument.
The 2nd and 3rd form of the constructor actually do accept an mxClassID argument; but those forms are supposed to be used for numeric arrays because they also want an mxComplexity parameter.
The 7th and 8th forms of the mwArray() constructor are what we are dealing with when creating structs, and the third argument is explicitly num_fields.
...
Further, mxClassID is an enumeration and the mxSTRUCT_CLASS value evaluates to 2, so maybe you were getting lucky by testing with two field names?
Co Melissant
Co Melissant 2014-8-20
编辑:Co Melissant 2018-11-13
indeed it was just luck, accessing any field other then the first two resulted in a crash. The third argument should indeed be the field count. Now got it working for big structure...
mwArray sub(1, 1, fieldCount, fieldNames)
works fine

请先登录,再进行评论。


Kaustubha Govind
Kaustubha Govind 2012-7-26
Does this work:
const char* data_fieldnames[] = {
"one", "two", "string", "subtest"
};
const char* sub_fieldnames[] = {
"test1", test2"
};
mwArray data(1,1,4,struct_fieldnames);
mwArray one(1);
mwArray two(2.0);
mwArray string("string");
mwArray sub(1,1,2,sub_fieldnames)
mwArray test1(1);
mwArray test2(2);
sub.Get("test1", 1,1).Set(test1);
sub.Get("test2", 1,2).Set(test2);
data.Get("one", 1,1).Set(one);
data.Get("two", 1,2).Set(two);
data.Get("string", 1,3).Set(string);
data.Get("subtest", 1,4).Set(sub);
mwString output = data.ToString();
const char* output_c = (const char*)output;
[Syntax reference here]
  2 个评论
SDS
SDS 2012-7-26
编辑:SDS 2012-7-26
Cleaned up the code a bit:
const char* data_fieldnames[] = {
"one", "two", "string", "subtest"
};
const char* sub_fieldnames[] = {
"test1", "test2"
};
mwArray data(1,1,4,data_fieldnames);
mwArray one(1);
mwArray two(2.0);
mwArray string("string");
mwArray sub(1,1,2,sub_fieldnames);
mwArray test1(1);
mwArray test2(2);
sub.Get("test1", 1,1).Set(test1);
sub.Get("test2", 1,2).Set(test2);
data.Get("one", 1,1).Set(one);
data.Get("two", 1,2).Set(two);
data.Get("string", 1,3).Set(string);
data.Get("subtest", 1,4).Set(sub);
mwString output_s = data.ToString();
const char* output_c = (const char*)output_s;
Oddly enough, I stepped through the code and found that this code quits and throws an exception when it tries to execute the second Get/Set line:
sub.Get("test2",1,2).Set(test2);
Kaustubha Govind
Kaustubha Govind 2012-7-30
Could you try looking inside the mwException to see what message the error contains.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Deploy to C++ Applications Using mwArray API (C++03) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by