9

Assume the following in Matlab:

%variable info contains a <1x2 struct>, so...
info(1,1);
info(1,2);

%these contains fields like .a .b etc.
info(1,1).a = [1, 2, 3, 4, etc... ];
info(1,2).b = [1, 2, 3, 4, etc... ];

Now in C#:

Normally, I would do something like:

//assume I received the variable info from an output parameter
//of a MatLab function, called via InterOp
MWNumericArray fieldA = (MWNumericArray) info.GetField("a");

//Remember that info contains 1row with 2 columns

I want to access the fields from both columns

//this is what i've tried, and failed, with  the exception for data["a",1]
MWNumericArray fieldA = (MWNumericArray) data["a", 0];
MWNumericArray fieldA = (MWNumericArray) data["a", 1, 1];
MWNumericArray fieldA = (MWNumericArray) data[0];

So how do I access a field from inside a nameless struct ?

While step debugging, VisualStudio defines info as a

info = { 1x2 struct array with fields: a b }
1
  • Just to be sure, are you satisfied with the answer you found? Commented Dec 4, 2013 at 15:19

2 Answers 2

7

Solved by using:

MWNumericArray fieldA = (MWNumericArray) data["a", 1]; //data(1,1).a
MWNumericArray fieldB = (MWNumericArray) data["b", 1]; //data(1,1).b
fieldA = (MWNumericArray) data["a", 2]; //data(1,2).a
fieldB = (MWNumericArray) data["b", 2]; //data(1,2).b

Remember mathematicians count from 1, programmers from 0.

Sign up to request clarification or add additional context in comments.

Comments

0

perhaps this can help :

MWNumericArray fieldA = (MWNumericArray) info.GetField("a"); //defines info as a
MWNumericArray fieldB = (MWNumericArray) info.GetField("b"); //defines info as b

MWArray resultA = fieldA[0];
MWArray resultB = fieldB[0];

show data:

System.out.println(fieldA);
System.out.println(fieldB);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.