Use a structure with fields in a loop - Outputs & indexing ??

조회 수: 7 (최근 30일)
Dimitris K
Dimitris K 2021년 9월 10일
댓글: Dimitris K 2021년 9월 10일
Hello everyone,
I am using a structure to output all my results from a function organised in different fields. My struct contains 10 fields - and as my loop has 101 steps my struct ends up being a struct array (1*101) with 10 fields.
Some of my fields are numeric arrays e.g. Sig (6*1) and i want to use the first value of each of these Sig in the 101 fields to plot them. How could i do this easily?
The way i thought is:
hist_st_cell=struct2cell(hist_state);
Sig_cell=hist_st_cell(1,1,:);
Sig_mat=cell2mat(Sig_cell);
Sig1_plot=Sig_mat(1,1,:);
Is there a better suggestion?

채택된 답변

Stephen23
Stephen23 2021년 9월 10일
Where S is your structure:
F = @(a)a.Sig(1);
V = arrayfun(F,S)
  댓글 수: 3
Stephen23
Stephen23 2021년 9월 10일
편집: Stephen23 2021년 9월 10일
The first line defines an anonymous function:
The anonymous function accepts one input "a" (which will be a scalar structure) and gets the first element of its field "Sig". The second line applies this function to each element of your structure array (remember each element of any array is a scalar array).
Dimitris K
Dimitris K 2021년 9월 10일
Thanks a lot for your answers !! It was very helpful!!!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by