Two different ways to create a struct have 2 different results

조회 수: 1 (최근 30일)
Vladimir
Vladimir 2013년 8월 22일
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