在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... ];
现在使用C#:
通常,我会做类似的事情:
//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
我想访问两列中的字段
//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];
那么如何从无名结构中访问字段?
在步骤调试中,VisualStudio将info
定义为
info = { 1x2 struct array with fields: a b }
答案 0 :(得分:7)
使用以下方式解决:
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
请记住,数学家从1开始计算,程序员从0开始算。
答案 1 :(得分:0)
也许这会有所帮助:
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];
显示数据:
System.out.println(fieldA);
System.out.println(fieldB);