Array indexing nested in a struct arrray

2 次查看(过去 30 天)
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.

采纳的回答

Chunru
Chunru 2022-9-22
编辑:Chunru 2022-9-22
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by