필터 지우기
필터 지우기

Array indexing nested in a struct arrray

조회 수: 2 (최근 30일)
Felix
Felix 2022년 9월 22일
편집: Chunru 2022년 9월 22일
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