Copying structure to structure

254 views (last 30 days)
I'm trying to copy structure fields from one struct to a new struct as:
W; %Is a struct with fields i.e. W.case(1).data1; W.case(2).data1..... etc.
d(10) = struct(); %Initializing the struct
for i1 = 1:10
d(i1) = W.field(i1)
end
But this doesn't work because "Subscripted assignment between dissimilar structures". Indexing struct "d" like this never work. So, what do I do? I can copy field by field but that seems rather tedious.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Nov 2020
d = struct('data1', {W.case.data1});
This would make d into a struct array with a field named 'data1' that held the case(:).data1 values.
If you have multiple fields then you can, for example,
d = struct('data1', {W.case.data1}, 'data2', {W.case.data2});
Are you looking to take all of the fields under W.case and make them into top-level fields ? If so then
d = cell2struct(struct2cell(W.case),fieldnames(W.case));
  2 Comments
Kristoffer Clasén
Kristoffer Clasén on 9 Nov 2020
Each field "case" contains hundreds of fields with data. I also need to copy the data in a certain order, but;
d = cell2struct(struct2cell(W.case(idx)),fieldnames(W.case));
.. works just fine. Thanks!
Walter Roberson
Walter Roberson on 9 Nov 2020
See also orderfields()

Sign in to comment.

More Answers (1)

Kristoffer Clasén
Kristoffer Clasén on 30 Nov 2020
So, back at it again with a similar problem but different situation. I want to "grow" a structure array while producing data, i.e.:
for i1 = 1:nel
data.a = load('blabla');
data.b = computations;
A(i1) = data
end
But this once again doesn't work since "Subscripted assignment between dissimilar structures", and this time I can't copy from one struct to another using cell2struct(struct2cell()) because I'm making the struct on the way. It works if I assign to the fields, i.e. A(i1).a = ..., A(i1).b = ..., but never directly to A(i1). I can store in a cell instead, A{i1}, but this cannot be converted to a struct using cell2struct since cell2struct doesn't recognize structs within a cell and not a cell with one dimension that's supposed to be converted into a structure array.
Am I forced to do a field-by-field conversion from cell to struct, assigning A(i1).x each time, or is there some kind of workaround to this? Its extremely annoying that I cant assign content to one structure array element at a time...
  2 Comments
Walter Roberson
Walter Roberson on 30 Nov 2020
I suspect that you oversimplified, as what you posted should work. Each time you are assigning on a struct whose fields have been created in the same order and there are the same number of fields with the same name. That would work.
What would not work would be if you had lifted the multiple fields resulting in data.a (because load creates one field per variable) into distinct fields.. e.g. if you had used the kind of cell2struct discussed above.
If you are creating fields within data that are named after dynamic fields, then in order to use those directly at the struct array level, you would have to have all of the field names stored for all the array elements. Such a thing could be done, but would it be a good idea, compared to pushing the dynamic parts one level lower in the struct array where it would not be complained about?
Kristoffer Clasén
Kristoffer Clasén on 30 Nov 2020
Yes, you are right, my example is extremely oversimplified. And you are right once again, it works if I incrementally add identical fields to the structure array.
I realize I could probably do that in my "main" code, just store all results from the loop temporarily in "data" and copy at the end of the loop. Problem is that in several cases my structure array "A" is already created and already contains fields, which means I cannot incrementally add new fields in the loop unless I create a new structure array in the loop and merge the old and new one once the new array is complete. I'd like to keep my structure as plain as possible since I work with the fields "manually" while postprocessing the data, that's why I prefer to skip additional intermediate fields just to make the array indexing work.
In the end, I think I understand the problem a bit better, and I think I got some ideas on how to tackle the problem...
Thanks Walter!

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!