Is there a specific standard for the rows and columns of a struct?

10 views (last 30 days)
In the book I read, there is no specific standard for the rows and columns of a struct. However, based on examples, it appears to follow the following pattern: For a struct of a "student" with multiple student records, the number of students determines the number of columns in the struct. For example, if there are records for three students, it would be a 1x3 struct. The number of fields in the struct is unrelated to the number of rows and columns. This can also be demonstrated with the following MATLAB example.(Structure with Multiple Fields
field1 = 'f1'; value1 = zeros(1,10);
field2 = 'f2'; value2 = {'a', 'b'};
field3 = 'f3'; value3 = {pi, pi.^2};
field4 = 'f4'; value4 = {'fourth'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
s=1×2 struct array with fields:
f1
f2
f3
f4
But then I found a counterexample on the same page,(Structure with One Field
field = 'f';
value = {'some text';
[10, 20, 30];
magic(5)};
s = struct(field,value)
s=3×1 struct array with fields:
So, is there a clear standard for the rows and columns of a struct? If there is a clear standard for rows and columns, then when mentioning a 1xn struct, I can envision a unique table in my mind! However, without a consistent standard, this will lead to confusion!

Answers (2)

Bruno Luong
Bruno Luong on 1 Sep 2023
Edited: Bruno Luong on 1 Sep 2023
You do whatever you prefer as programmer, there is no "standard" as long it is allowed by the language. This applies for array, struct array, cell array, string array, class array etc...

Fangjun Jiang
Fangjun Jiang on 1 Sep 2023
It is described in the Help document of struct().
The size of the resulted struct array is the same as the size of the cell array specified in the "value" input.
s = struct(field1,value1,...,fieldN,valueN) creates a structure array with multiple fields.
  • If none of the value inputs are cell arrays, or if all value inputs that are cell arrays are scalars, then s is a scalar structure.
  • If any of the value inputs is a nonscalar cell array, then s has the same dimensions as that cell array. Also, if two or more value inputs are nonscalar cell arrays, then they all must have the same dimensions.For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s. For example, s = struct('x',{'a','b'},'y','c') returns s(1).x = 'a', s(2).x = 'b', s(1).y = 'c', and s(2).y = 'c'.
  • If any value input is an empty cell array, {}, then output s is an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use [] as a value input instead.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!