Two different ways to create a struct have 2 different results

1 次查看(过去 30 天)
Although it's documented in the struct help, I'm curious if someone can explain the reasoning. There are 2 ways to create a struct, (1) dynamically adding fields and assigning their values and (2) using struct('field1', value1, 'field2', value2, ... ).
(1):
code.metaInfo = getEmptyMetaInfo();
code.dataValid = false;
code.dataCodewords = cell(MAXROWS, MAXCOLUMNS);
The above results in:
code =
metaInfo: [1x1 struct]
dataValid: 0
dataCodewords: {90x30 cell}
(2):
code = struct('metaInfo', getEmptyMetaInfo(), 'dataValid', false, 'dataCodewords', cell(MAXROWS, MAXCOLUMNS));
But this results in...
code =
90x30 struct array with fields:
metaInfo
dataValid
dataCodewords
I would expect the result to be exactly the same (before reading struct's documentation). Why aren't they?

回答(1 个)

Iain
Iain 2013-8-22
I think it may be because structs are an alternative to cells and the chap who wrote the function thought it might make be sensible to distribute out the contents of cell arrays in such a fashion.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by