Two different ways to create a struct have 2 different results

1 view (last 30 days)
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?

Answers (1)

Iain
Iain on 22 Aug 2013
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.

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!