understanding contents and size of structures

41 visualizzazioni (ultimi 30 giorni)
Leonardo Wayne
Leonardo Wayne il 18 Mar 2016
Commentato: Adam il 18 Mar 2016
what does 3x1 in the description of a struct mean? also what would be the difference in a 1x3 structure. If you can provide a clarifying example.

Risposte (2)

Adam
Adam il 18 Mar 2016
Modificato: Adam il 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.
>> size(s)
ans =
1 3
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 struct
doc 'Create a structure array'
  2 Commenti
Leonardo Wayne
Leonardo Wayne il 18 Mar 2016
I have the structure called niChData in the image attached. I am trying to get entire first column putting the following command in the command window works, niChData(1:8).signalLogged; however I would like to assing it to a variable:example a = niChData(1:8).signalLogged;
this only returns first value in the first column (1). how to get all the column values.
Adam
Adam il 18 Mar 2016
a = [ niChData(1:8).signalLogged ];
should work.

Accedi per commentare.


Walter Roberson
Walter Roberson il 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.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by