rmfield / clear structur entry ?

hey Guys, I have got a 1xn structure. Now i just want to write a code which can delete on column of a structure. For example:
A is structure with A.x / A.y /A.z This is a 1x5 structure. Now I want to make this a 1x4 structure by deleting the 3rd Column. So A(3) is gone and A(4) is now the new A(3) / A(5) is the new A(4)

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 9월 12일

0 개 추천

A=struct('x',num2cell(rand(1,5)),'y',num2cell(rand(1,5)),'z',num2cell(rand(1,5)))
A(3)=[]

댓글 수: 5

Max Müller
Max Müller 2014년 9월 12일
편집: Max Müller 2014년 9월 12일
tried this already...somehow it doesnt work
for u = 1:length(A)
Check = A(u).UsedAmp + A(u).OptimalAmp + A(u).ClosestAmp;
if Check == 0;
A(u) = [ ];
end
end
Pierre Benoit
Pierre Benoit 2014년 9월 12일
편집: Pierre Benoit 2014년 9월 12일
It may not work because each time you set A(u) to 0, the length of A will reduce, so you will exceed matrix dimensions at some point.
Try this :
Check = [A.UsedAmp] + [A.OptimalAmp] + [A.ClosestAmp];
A(Check == 0) = [];
If it's still doesn't work, it may be that you didn't define your condition correctly.
Max Müller
Max Müller 2014년 9월 12일
Your are right! If Check == 0 deletes one part of the structure, it exceedes the matrix dimension.
A=struct('UsedAmp',num2cell([1 2 3 4 5]),'OptimalAmp',num2cell([-1 2 3 4 -3]),'ClosestAmp',num2cell([0 2 3 4 -2]))
idx=[];
for u = 1:numel(A)
Check = A(u).UsedAmp + A(u).OptimalAmp + A(u).ClosestAmp;
if Check == 0;
idx(end+1)=u
end
end
A(idx)=[]
Azzi Abdelmalek
Azzi Abdelmalek 2014년 9월 12일
편집: Azzi Abdelmalek 2014년 9월 12일
Or
A=struct('UsedAmp',num2cell([1 2 3 4 5]),'OptimalAmp',num2cell([-1 2 3 4 -3]),'ClosestAmp',num2cell([0 2 3 4 -2]))
A(squeeze(sum(cell2mat(struct2cell(A))))==0)=[]

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

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2014년 9월 12일

편집:

2014년 9월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by