String 包含字符串和单元格的结构的MATLAB行为
我想要一个包含字符串的结构和字符串的单元格数组 这与预期的效果一样:String 包含字符串和单元格的结构的MATLAB行为,string,matlab,struct,cell,String,Matlab,Struct,Cell,我想要一个包含字符串的结构和字符串的单元格数组 这与预期的效果一样: x = struct('field1','one','field2','two'); >> x.field1 ans = one 但只要我有一个字符串单元格数组,这就是x.field1的输出: x = struct('field1','one','field2',{'two','three'}); >> x.field1 ans = one ans = one 您能告
x = struct('field1','one','field2','two');
>> x.field1
ans =
one
但只要我有一个字符串单元格数组,这就是x.field1
的输出:
x = struct('field1','one','field2',{'two','three'});
>> x.field1
ans =
one
ans =
one
您能告诉我如何将字符串和单元格数组正确地组合到一个结构中吗?谢谢 在单元格数组周围添加两个大括号,以防止Matlab将其内容分发到结构数组中:
>>x = struct('field1','one','field2',{{'two','three'}})
x =
field1: 'one'
field2: {'two' 'three'}
我试过使用八度音阶,这样的定义似乎会导致1x2结构数组,与field1相关的两个条目的值都是“一”。如果您将x定义为以下内容,则情况有所不同:
x=struct('field1','one','field2',struct('subfield1','two','subfield2','three')代码>。