필터 지우기
필터 지우기

Modify values in a struct array

조회 수: 22 (최근 30일)
DC
DC 2020년 4월 1일
댓글: DC 2020년 4월 1일
I would like to modify the values of a field of a struct array (without using a for loop). If my initial struct array is the following:
a.b(1).c = 5;
a.b(2).c = 5;
a.b(3).c = 5;
I would like to modify it such that:
a.b(1).c = 1
a.b(2).c = 2
a.b(3).c = 3
I thought that
[a.b.c] = [1:3]
would work, but it doesn't: Insufficient number of outputs from right hand side of equal sign to satisfy assignment.

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 1일
newvalues = 1:3;
temp = num2cell(newvalues);
[a.b.c] = temp{:};
Unfortunately this is not easy to do on one line, at least not without using a helper function.
Expand = @(temp) temp{:};
[a.b.c] = Expand(num2cell(1:3));
  댓글 수: 1
DC
DC 2020년 4월 1일
Thank you, that works!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by