Building a mwArray Structure from C++
13 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
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
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
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
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;
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!