MATLAB Answers

Vojtech
0

Get line of vector from struct array field

Asked by Vojtech
on 16 Apr 2018
Latest activity Edited by Stephen Cobeldick on 16 Apr 2018
Let's have a structure:
my_struct(1).my_field = [11;12];
my_struct(2).my_field = [21;22];
I want to get vector of first values of the field 'my_field', that is:
temp = [my_struct.field];
result = temp(1,:);
Is there any more elegant way which does not use temporary variable and might be coded into one line? Something like:
result = [my_struct.field](1,:); % produces error

  0 Comments

Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 16 Apr 2018
Edited by Stephen Cobeldick on 16 Apr 2018
 Accepted Answer

"Is there any more elegant way which does not use temporary variable and might be coded into one line?"

No.

You could use arrayfun, but in my opinion this is much less elegant:

>> arrayfun(@(s)s.my_field(1),my_struct)
ans =
   11   21

  0 Comments

Sign in to comment.