필터 지우기
필터 지우기

How to add values to structure array without loop

조회 수: 3 (최근 30일)
Durga Lal Shrestha
Durga Lal Shrestha 2015년 12월 8일
댓글: Somaye Hamedi Bazaz 2018년 11월 24일
Consider the following loop:
values = [2 5 6 4 8 9 10 5 15 7]
for i=1:10
arr(i).myfield = values(i);
end
How can this be done without loop?
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 8일
T = struct('myfield', num2cell(values));
arr(1:length(T)) = T;
In the special case that arr does not already exist, it can be done with the one line
arr = struct('myfield', num2cell(values));
  댓글 수: 4
Durga Lal Shrestha
Durga Lal Shrestha 2015년 12월 8일
Thanks. It works now.
Somaye Hamedi Bazaz
Somaye Hamedi Bazaz 2018년 11월 24일
Great!!!!!!!!!!!! thank you

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by