Building a mwArray Structure from C++
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (2)
Co Melissant
il 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 Commenti
Will Grant
il 22 Lug 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
il 20 Ago 2014
Modificato: Co Melissant
il 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
Kaustubha Govind
il 26 Lug 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;
Vedere anche
Categorie
Scopri di più su Deploy to C++ Applications Using mwArray API (C++03) in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!