understanding contents and size of structures
105 views (last 30 days)
Adam on 18 Mar 2016
Edited: Adam on 18 Mar 2016
3x1 and 1x3 mean the same for structs as they do for numeric or cell arrays - i.e. you have an array of structs. e.g.
s(1).a = 7;
s(2).a = 8;
s(3).a = 9;
will give you an array of 3 elements, each of which is a struct with field 'a'. Note that unlike cell arrays though all elements of a struct array must be homogeneous. So if you then do, for example
s(3).b = 10;
it will add a 'b' field to every structure in the array, those in elements 1 and 2 of the array will have  (empty) assigned to 'b'.
1x3 or 3x1 is just the difference between a column or a row. With structs the difference is usually irrelevant as you aren't using them directly for maths operations, but the behaviour is as normal - e.g. you can concatenate row arrays of structs together into a longer row.
By default using the above syntax you will always get a 1xn row array, but if you want a 3x1 you can instead use
s(1,1).a = 7;
s(2,1).a = 8;
s(3,1).a = 9;
More information can be found at
doc 'Create a structure array'
Walter Roberson on 18 Mar 2016
3 x 1 means that if you use multidimensional indexing for the struct instead of linear indexing, then the first dimension is length 3 and the second is length 1.
Although it is common to use linear indexing for structs, there are other uses where it makes sense to use multidimensional indexing.
Imagine, for example, that you are doing finite element modeling, then you might have a 2 dimensional or 3 dimensional set of points being modeled. Each point might have several pieces of information associated, perhaps of different data types, so it might make sense to use a struct to hold the data. In a rigid mesh situation, there could a one-to-one relationship between 2 dimensional or 3 dimensional coordinates and the structure of data associated for that coordinate.
In the FEM case, a different way of managing the data might be very reasonable, where you have one variable per property and that variable is indexed by the coordinates. But now extend to the case where not all the nodes have the same properties. But not always: if two nodes can exchange properties then it can be a lot more convenient to move the structure of data than to run through all of the variables. And also, the case with structures is easier to generalize: if you move the structs rather than dig through all the variables then as you add new properties you do not need to update all of the code that runs through individual variables acting on each of them.