Filter löschen
Filter löschen

Array indexing nested in a struct arrray

2 Ansichten (letzte 30 Tage)
Felix
Felix am 22 Sep. 2022
Bearbeitet: Chunru am 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.

Akzeptierte Antwort

Chunru
Chunru am 22 Sep. 2022
Bearbeitet: Chunru am 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by