Building a mwArray Structure from C++

13 views (last 30 days)
SDS
SDS on 26 Jul 2012
Edited: Co Melissant on 13 Nov 2018
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.

Answers (2)

Co Melissant
Co Melissant on 17 Apr 2014
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 Comments
Will Grant
Will Grant on 22 Jul 2014
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 on 20 Aug 2014
Edited: Co Melissant on 13 Nov 2018
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

Sign in to comment.


Kaustubha Govind
Kaustubha Govind on 26 Jul 2012
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 Comments
SDS
SDS on 26 Jul 2012
Edited: SDS on 26 Jul 2012
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 on 30 Jul 2012
Could you try looking inside the mwException to see what message the error contains.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!