Comparing the two structure

586 vues (au cours des 30 derniers jours)
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan le 18 Juin 2015
Commenté : Arwel le 25 Nov 2021
I need to compare the two structure and remove the matching fieldname. Is it possible?
How can i do it?
Thanks a lot
  2 commentaires
B.k Sumedha
B.k Sumedha le 18 Juin 2015
Yes.It can be done.
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan le 18 Juin 2015
How to do it?? Thanks

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 18 Juin 2015
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 commentaires
Gopalakrishnan venkatesan
Gopalakrishnan venkatesan le 18 Juin 2015
Thank u
Arwel
Arwel le 25 Nov 2021
nice..

Connectez-vous pour commenter.

Plus de réponses (1)

B.k Sumedha
B.k Sumedha le 18 Juin 2015
Modifié(e) : B.k Sumedha le 18 Juin 2015
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 commentaires
B.k Sumedha
B.k Sumedha le 18 Juin 2015
Cant isequlan function compare two structures??
Guillaume
Guillaume le 18 Juin 2015
Modifié(e) : Guillaume le 18 Juin 2015
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by