Azzera filtri
Azzera filtri

Array indexing nested in a struct arrray

2 visualizzazioni (ultimi 30 giorni)
Felix
Felix il 22 Set 2022
Modificato: Chunru il 22 Set 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.

Risposta accettata

Chunru
Chunru il 22 Set 2022
Modificato: Chunru il 22 Set 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

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by