Creating an array of structs and using the field directly?

Hey guys. I'm wondering if there is a workaround for doing something like this:
a = {[struct('field',1) struct('field',2)].field}
which works in octave but not in matlab ("invalid syntax at '.'. Possibly a ')', ']' or '}' is missing"), I have to use a temporary variable. This is quite annoying. Is there a workaround?
Thanks!

 Accepted Answer

It is not clear why you need to structure at all, as the output is simply a cell array with some numeric scalars in it (Octave code below)
>> {[struct('field',1) struct('field',2)].field}
ans =
{
[1,1] = 1
[1,2] = 2
}
>> {struct('field',{1,2}).field}
ans =
{
[1,1] = 1
[1,2] = 2
}
>> {1,2}
ans =
{
[1,1] = 1
[1,2] = 2
}
If the input is already a structure, then you can simply access the field values using the methods given in the documentation for non-scalar structures:
{X.field}

6 Comments

Thank you! You pointed out correctly, that the input already is a struct coming from a source I can't manipulate. It gets even worse, in reality the the struct arrays are coming from a cell:
y = {([this.sectorInformation{:}]).idxToSector};
debug> whos this.sectorInformation
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
f this.sectorInformation 1x2 4640 cell
debug> whos this.sectorInformation{1}
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
f this.sectorInformation{1} 1x1 2320 struct
I realized using cell2mat could replace the [this.sectorInformation{:}] thing, but I need to introduce another variable, too.
I think a temporary variable would be the simplest. And teach the author of the that source code to use non-scalar arrays properly.
How could this be done here?
Good question, but as you have only given us one line of code it is impossible for us to know what the entire code is doing. All we know is that your code concatenates two scalar structures together, so most likely there are more efficient ways of handling this. Without any explanation of what the code does and its requirements there is not much point to giving any more detailed advice.
Sorry, you're right of course. But you explicitely mentioned the "proper usage of non-scalar arrays", so I thought there is a obvious mistake?
Again it depends on what you mean by "mistake": perhaps you need to have two scalar structures, this I do not know. See my answer for one way of defining a non-scalar structure using a single call to struct.

Sign in to comment.

More Answers (1)

As long as you insist on using standard struct : No, there is no workaround. Using a temporary variable is not a big deal and does not waste additional resources.
You can create your own object class, which solves the problem. But this will be less efficient.

3 Comments

Okay thank you. Of course using a temporary variable isn't that big deal, it's just for clear- and mayby laziness ;)
Can I accept both answers as correct answers, since they both are correct?
You can accept one answer only. But you can vote for more answers and a comment is useful also.

Sign in to comment.

Asked:

on 19 Nov 2015

Commented:

Jan
on 23 Nov 2015

Community Treasure Hunt

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

Start Hunting!