필터 지우기
필터 지우기

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