MATLAB Answers

Accessing all lower elements of a structure

31 views (last 30 days)
I'm attempting to set up my data in a format that makes it easy to access and understand, so I have utilized structures. Each 'variable' is a structure that has has a field for name, value, units, and a description. Then the sets of variables are organized into groups that make sense for my application. However, I am having issues accessing all elements of those groups. I would like to display the data in a table inside of a GUI, so I wanted to access the underlying elements by what would be in each column (name, value, unit, description), here is a simple example:
>> = 'My name is foo';
>> = 'My name is bar';
>> test.group1(:).name
Reference to non-existent field 'name'.
So there will be several groups, and they will have many variables (foo & bar in the example) but each variable will have a name that I'd like to access without a for-loop. In cells I was able to make use of the {:} operator to access each element of the cell individually, so I expected similar functionality for a structure.
Any help would be appreciated.

  1 Comment

Shane Smith
Shane Smith on 3 Oct 2018
I will use classes instead, that should strike a good balance of making the code easy to follow and modify, and making it work well.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 2 Oct 2018
Edited: Stephen Cobeldick on 3 Oct 2018
"so I expected similar functionality for a structure."
There are easy ways to access structures, but not with the kind of "all fields" syntax that you are trying. I highly recommend that you do not nest structures, but use simple non-scalar structures, then you can indeed do things like this:
>> S(1).name = 'foo';
>> S(2).name = 'bar';
>> {}
'foo' 'bar'
Read more about how this works:
You can also use getfield and setfield to efficiently work with structures. The examples in the documentation are particularly useful.
You will note that your example cell array syntax C{:} generates a comma-separated list from a cell array C, just like S.field generates a comma-separated list from the structure S. If you really want to use nested structures, then you can use this method after putting the child structure into a temporary variable:
>> S.Z = 'hello';
>> S.A(1).B = 1;
>> S.A(2).B = 2;
>> tmp = S.A;
>> tmp.B
ans = 1
ans = 2
By now you will also realize that your question "Accessing all lower elements of a structure" is what my answer shows you, e.g the structure tmp has two elements and one field, while the structure S has one element and two fields. The number of elements of a structure depends solely on its size, just like any other array. The number of fields of a structure is totally independent of its size. So your example syntax (where you use (:) for "all fields") is not related the elements_of the structure but to the _fields of the structure group1.


Shane Smith
Shane Smith on 2 Oct 2018
The nested structure makes the code a lot easier to understand than using non-scalar structures. For example, I use the variable 'length' for several different groups, trying to access this with numbers makes it confusing as to what length you are accessing, for example: vs. car(26).pressure
In my case each group has about 30 variables, so I was trying to make it simple to access and understand what you were using or seeing.
Stephen Cobeldick
Stephen Cobeldick on 2 Oct 2018
@Shane Smith: in a situation like that I would put all of the data into one ND array, where each dimension encodes some feature. The reason is that simpler data design makes the code much simpler. What you are doing will work, but you will have to use loops or getfield to access any one single value... this means you miss out on all of the advantages of using a high-level language like MATLAB, where functions and operators that can quickly process entire arrays all at once.
However if using nested structures makes your code or data processing simpler, then by all means use them.
Note another option would be to define classes to represent that data.

Sign in to comment.

More Answers (0)

Sign in to answer this question.