0

when I do this:

pedigree = struct('parents', [0,0;1,3;0,0]);
pedigree.names = {'Ira','James','Robin'};

I get a different thing to when I do this:

pedigree = struct('parents', [0,0;1,3;0,0], 'names', {'Ira','James','Robin'});

What are the two things, how are they different, is there a literal form for whatever is made by the first thing, and more generally, what are the words for whatever is going on?

4

3 回答 3

1

问题是结构也是一个数组。struct当您使用命令创建结构时

pedigree = struct('parents', [0,0;1,3;0,0], 'names', {'Ira','James','Robin'});

each 的值应该是一个元胞数组,一个元胞数组的每个元素都被分配。这样做是将矩阵分别设置[0 0; 1 3; 0 0]为 all pedigree.parents、和IraJamesRobinpedigree(1).names和。要做你想做的,只需传递一个带有单个元素的单元格数组:pedigree(2).namespedigree(3).names

pedigree = struct ("parents", [0,0;1,3;0,0],
                   "names", {{'Ira','James','Robin'}});

请注意元胞数组本身如何包含元胞数组,因此在这种情况下,您有一个具有单个元素的元胞数组(也是一个元胞数组)。

于 2013-04-25T21:20:48.343 回答
-1

在这里可以找到很多启示:

https://www.gnu.org/software/octave/doc/interpreter/Data-Containers.html#Data-Containers

于 2013-04-28T21:12:11.283 回答
-1

在这里可以找到很多启示:

http://abandonmatlab.wordpress.com/2009/08/12/programatically-creating-a-struct/

于 2013-04-29T04:32:27.850 回答