MATLAB: Assign the content of a structure to another

assignstructure

I have two structures A and B having the same fields but not the same sizes. The size of A can be bigger or smaller than B depending on the code. A is originally defined and I want to change its content at the end of the round to use what is in B. So what I want to do is to assign the content of B to A and if there are more fields in A , remove them. If there is less add the missing ones.
I tried
[A]= B;
It does not give me any error but the result I get is not correct. Then I tried something like
[A(1:numel(B)).field1]=B.field1;
...
[A(1:numel(B)).fieldn]=B.fieldn;
but I get an error because both structures do not have the same number of elements.
I tried also
[A(1:length(B)).field1]=B.field1;
No errors but the result is not correct either. What is the right way to it? And why the first one does not work?

Best Answer

Turns out that there was a letter in capital in one of the fields which I mistakenly wrote as lower case and it was the source of all the problems. The assignment a=b is correct and was not the source of the problem. Still I am going to leave the question since it might come handy to someone else.