How to concatenate two structures of different size?
Afficher commentaires plus anciens
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 commentaire
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 31 Déc 2015
0 votes
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 commentaires
etudiant_is
le 31 Déc 2015
Image Analyst
le 31 Déc 2015
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.
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!