How to concatenate two structures of different size?

18 次查看(过去 30 天)
Hi to all I have something like this
function res=newRessource (size)
for k=1: size
res.a(k).v1= val1;
res.a(k).v2= val2;
end
then i have
res1= newRessource (size1);
res2= newRessource (size2);
After that in a for loop the content of the attributes of res1 are changed at each iteration and its size also and What i want to do, at the end of each iteration of the for loop, is to get the old content of res2 concatenated with the content of res1 because i will need to do some actions based on it later
I tried
res2 = [res2, res1];
but i get an error "Error using Horizcat Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields."
Any help on how to solve this? Thanks
  1 个评论
Stephen23
Stephen23 2015-12-31
Note that you should never use the name size for any variable or function name. This shadows the very important inbuilt function size and stops it from working. For the same reason you should never use the variable names length, i, j, dir, struct, ans, etc.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2015-12-31
You are changing the set of fields.
The below will merge the two by creating the missing fields.
res1fields = fieldnames(res1);
res2fields = fieldnames(res2);
%add fields that are in res2 but not in res1
res1copy = res1;
for K = 1 : length(res2fields)
thisfield = res2fields{K};
if ~ismember(thisfield, res1fields)
res1copy(1).(thisfield) = []; %create the field, make it empty
end
end
%add fields that are in res1 but not in res2
res2copy = res2;
for K = 1 : length(res1fields)
thisfield = res1fields{K};
if ~ismember(thisfield, res2fields)
res2copy(1).(thisfield) = []; %create the field, make it empty
end
end
%now do the appending
res2 = [res2copy, res1copy];
  3 个评论
Walter Roberson
Walter Roberson 2015-12-31
If you had used res1copy.(thisfield) then that would be equivalent to
[res1copy(1).(thisfield), res1copy(2).(thisfield), res1copy(3).(thisfield), .... res1copy(end).(thisfield)]
because of structure expansion (which is similar to cell array expansion). You do not want that to happen as you only have a single [] to assign to those length(res1copy) different locations.
res1copy(1).(thisfield) on the other hand refers only to a single location so it only needs a single value to assign. But a side effect of assigning a new field into a single structure array member is that all of the other members of the structure array also get the field created (and assigned an empty value). So res1copy(1).(thisfield) = [] would have the same effect as
for K = 1 : length(res1copy)
res1copy(K).(thisfield) = [];
end
but is much more efficient.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-12-31
It worked for me. Of course I changed the input variable from size, which is a built-in function that you should NEVER use as the name of your variable.
The problem it's saying you have is that either res1 or res2 has more than one field. Your function creates a structure res, and res has one field, called "a". Now "a" is actually a structure of arrays, but as far as res is concerned, res just has one named field. So let's say there is a res1.a, but you have 2 fields on res2: a res2.a and a res2.b. So now it doesn't know how to concatenate them to make a new variable since "Concatenation of structure arrays requires that these arrays have the same set of fields." It seems like you'll have to have them both have the same set of named fields.
  2 个评论
etudiant_is
etudiant_is 2015-12-31
you are right, i only put size to make the example clear (i forgot it was already a built-in function). But the problem was not coming from that.
Image Analyst
Image Analyst 2015-12-31
Actually the problem was coming from the second paragraph in my answer - missing fields - perhaps you didn't read that far. And Walter gave you a solution "by creating the missing fields" so now you should be all set.

请先登录,再进行评论。

类别

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