Comparing the two structure

조회 수: 598 (최근 30일)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2015년 6월 18일
댓글: Arwel 2021년 11월 25일
I need to compare the two structure and remove the matching fieldname. Is it possible?
How can i do it?
Thanks a lot
  댓글 수: 2
B.k Sumedha
B.k Sumedha 2015년 6월 18일
Yes.It can be done.
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2015년 6월 18일
How to do it?? Thanks

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 18일
a=struct('field1',1,'field2',2,'field3',5)
b=struct('field1',3,'field3',4,'field4',6)
f1=fieldnames(a)
f2=fieldnames(b)
f=intersect(f1,f2)
a=rmfield(a,f)
b=rmfield(b,f)
  댓글 수: 2
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan 2015년 6월 18일
Thank u
Arwel
Arwel 2021년 11월 25일
nice..

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

추가 답변 (1개)

B.k Sumedha
B.k Sumedha 2015년 6월 18일
편집: B.k Sumedha 2015년 6월 18일
U can use the isequal command to compare between structs.
tf = isequaln(A,B)
And if the result is 1,u can remove those fieldname like for example:
function B = column_removal(A,n)
A = [1 2 3; 4 5 6];
n=2;
A(:,n)=[];
B = A;
end
In this example u will be removing the 2nd coulmn.
  댓글 수: 3
B.k Sumedha
B.k Sumedha 2015년 6월 18일
Cant isequlan function compare two structures??
Guillaume
Guillaume 2015년 6월 18일
편집: Guillaume 2015년 6월 18일
Yes, it can, and it will tell you whether the two structures have the exact same field names and field values.
It won't give you the list of fields that have the same name.
Your example of column removal has nothing to do with structures.
Note: I'm not belittling your answer as the OP is obviously happy with it since he accepted it. But if somebody searches the forum for a way to remove matching field names, I'd rather they went to the correct answer to that problem.

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

카테고리

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