How to access different FIELD in structure using CELLFUN

조회 수: 5 (최근 30일)
balandong
balandong 2017년 11월 28일
답변: balandong 2017년 11월 30일
Dear all, the objective was to access multiple fields (3 fields) located in multiple cell container. CELLFUN was used to make the coding more compact. The data from the 3 fields from each cell were then combine in a matrix. The code below demonstrated clearly what I tried to achieve. However, as can be seen below, the code is very lengthy. I wonder if this code can be further simplified by utilizing the CELLFUN, or any suitable FUNCTION. I really appreciate for any tips.
The end result should that like EachComb CELL
load('help_strcCellFun.mat');
vec_SBN_table=cellfun(@(S) S.SBN, result, 'uni',true);
nSizeComb=size(vec_SBN_table,2); % Equal to s.TotalCombntn
C_x=1;
for f_x=1:nSizeComb
xx_TPR{C_x}= vec_SBN_table(f_x).TPR ;
xx_TNR{C_x}= vec_SBN_table(f_x).TNR ;
xx_ACC{C_x}= vec_SBN_table(f_x).ACC ;
C_x=C_x+1;
end
nSizeCPT_ROW=size(s.CptPair,1);
yy_SeqNo=repmat((1:nSizeCPT_ROW)',1,s.TotalCombntn);
yy_TPR=reshape(horzcat(xx_TPR{:}),nSizeCPT_ROW,[]);
yy_TNR=reshape(horzcat(xx_TNR{:}),nSizeCPT_ROW,[]);
yy_ACC=reshape(horzcat(xx_ACC{:}),nSizeCPT_ROW,[]);
EachComb=cell(nSizeComb,1);
for f_x=1:nSizeComb
comb(:,1)=yy_SeqNo(:,f_x); % Sequence No
comb(:,2)=yy_TPR(:,f_x);
comb(:,3)=yy_TNR(:,f_x);
comb(:,4)=yy_ACC(:,f_x);
EachComb{f_x,1}= comb;
end
  댓글 수: 2
per isakson
per isakson 2017년 11월 30일
I don't think the mat-file matches the code. For example, it doesn't contain S.SBN
balandong
balandong 2017년 11월 30일
Hi Per, The capital S is the Variable (I am dont know the actual name for it) similar to x as in the following line.
Cpositives = cellfun(@(x) x(x>0), C, 'UniformOutput',false);

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

답변 (1개)

balandong
balandong 2017년 11월 30일
The following line were much more shorter than the originally proposed.
load('help_strcCellFun.mat');
vec_SBN_TPR=cellfun(@(S) S.SBN.TPR, result, 'uni',false);
vec_SBN_TNR=cellfun(@(S) S.SBN.TNR, result, 'uni',false);
vec_SBN_ACC=cellfun(@(S) S.SBN.ACC, result, 'uni',false);
endExtractedData = arrayfun(@(k) [vec_SBN_TPR{k}; vec_SBN_TNR{k};vec_SBN_ACC{k}]',1:s.TotalCombntn,'un',0);

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by