mwArray structure index explaination

Hello I am a little bit confused about the documentation of mwArray. Let's say I have structure array like
const char *field_name[] = {"a", "b", "c", "d"};
mwArray structure(2,1,4,field_name);
the question is how can I set value to structure(1).a and structure(2).a respectively? what the index should be in structure.Get() function? Thanks!

回答 (1 件)

Adam
Adam 2016 年 6 月 9 日
編集済み: Adam 2016 年 6 月 9 日

0 投票

I tend to use
mxCreateStructMatrix
to create a struct, but I guess your syntax works just as well, judging from the help. Then you can use either of these
mxSetFieldByNumber
mxSetField
to set a field e.g.
const char* axisFieldNames[] = { "x", "y", "z" };
mxArray* labelsStruct = mxCreateStructMatrix( 1, 1, 3, axisFieldNames );
mxSetFieldByNumber( labelsStruct, 0, 0, "someStringForX" );
mxSetFieldByNumber( labelsStruct, 0, 1, "someStringForY" );
mxSetFieldByNumber( labelsStruct, 0, 2, "someStringForZ" );
If you have an actual array of structs as you do then of course you will want to fill in values for indices other than 0 in the 2nd argument. In my code where I took this example from I was just creating a scalar struct.

2 件のコメント

eey
eey 2016 年 6 月 10 日
Hi, thanks for your reply, but I was talking about mwArray and the question was how to access different element if I have a structure array and set values for the same field in different elements.
Adam
Adam 2016 年 6 月 10 日
Change the 2nd argument to mxSetField or mxSetFieldByNumber or mxGetFieldByNumber or mxGetField

サインインしてコメントする。

カテゴリ

質問済み:

eey
2016 年 6 月 9 日

コメント済み:

2016 年 6 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by