Array indexing nested in a struct arrray

4 views (last 30 days)
Felix
Felix on 22 Sep 2022
Edited: Chunru on 22 Sep 2022
I am trying to directly address a value of an array in a struct array.
For example, I want the second element in each case, the output would be:
ans =
2
5
I have solved it at the moment with nested for-loops, but maybe there is a smarter solution
myTable(1).name='A';
myTable(1).data=[1:3];
myTable(2).name='A';
myTable(2).data=[4:6];
myTable(3).name='B';
myTable(3).data=[7:9];
myTable([myTable.name]=='A').data(2)
Intermediate dot '.' indexing produced a comma-separated list with 2 values, but it must produce a single value when followed by subsequent indexing operations.

Accepted Answer

Chunru
Chunru on 22 Sep 2022
Edited: Chunru on 22 Sep 2022
myTable(1).name='A';
myTable(1).data=[1:3];
myTable(2).name='A';
myTable(2).data=[4:6];
myTable(3).name='B';
myTable(3).data=[7:9];
%myTable
a = arrayfun(@(x) x.data(2), myTable([myTable.name]=='A'))
a = 1×2
2 5

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!