Concatenate same fields in multiple structures in a loop using field names

21 Ansichten (letzte 30 Tage)
I have several .mat files and each is a structure with the same fields. For each field, I'd like to create an array that has the name of that field and is the concatenation of all structures with that field name. I can provide a psuedo-code, but not sure how to write it out for real:
firststruct has field names 'one' and 'two'
secondstruct has field names 'one' and 'two'
for i = 1:number of structure files I have (the .mat files)
get the field names
for j = 1:number of field names
'this would be the name of the first field (so 'one')' = this would create a vector that concatenates all .one fields;
'this would be the name of the second field (so 'two')' = this would create a vector that concatenates all .two fields;
end
end
To try to be clearer, if firststruct.one = [1,2] and secondstruct.one = [3,4], the result I'm hoping for is one = [1,2,3,4].
  3 Kommentare
Daniel
Daniel am 26 Mai 2020
Bearbeitet: Daniel am 26 Mai 2020
I see what you're saying. I guess I'm just trying to avoid typing as much by eliminating the structure format and just having a bunch of arrays that have the names of the fields. Is there something like struct2array that does essentially that? If so, could that be combined with your answer below to make a structure that concatenates the same fields from multiple structures and then converts that structure to a bunch of arrays?
Stephen23
Stephen23 am 26 Mai 2020
There are two possible situations:
1- you know all of the fieldnames in advance, in which case it is simple to explicitly allocate them to variables:
one = S.one;
two = S.two;
2- you don't know the fieldnames, in which case see previous comment. Note that having an unknown number of variables with unknown names in the workspace makes them difficult to process, increases the risk of bugs (e.g. overwriting a variable without any warning), and makes debugging difficult.
Personally I would just leave them in the structure: that little bit of extra typing makes the data easy to track and the code easy to maintain.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 26 Mai 2020
Bearbeitet: Stephen23 am 26 Mai 2020
Because all of the structures contain the same fieldnames they should be stored as one non-scalar structure:
>> S(1).one = [1,2];
>> S(1).two = NaN;
>> S(2).one = [3,4];
>> S(2).two = Inf;
Then the concatenation can be done in just a few lines:
>> C = struct2cell(S);
>> C = cellfun(@(c)[c{:}],num2cell(C,2:3),'uni',0);
>> Z = cell2struct(C,fieldnames(S),1);
>> Z.one
ans =
1 2 3 4
>> Z.two
ans =
NaN Inf
EDIT: to load multiple mat files, where each contains one structure of unknown name:
for k = 1:numel(...)
F = ... filename
T = struct2cell(load(F));
S(k) = T{1};
end
  7 Kommentare
Daniel
Daniel am 26 Mai 2020
Hey, I know you're trying to help and I appreciate it, I really do, but we're not all in this to become expert coders. I tried your way and it didn't do what I wanted. That's probably because of something else I did, but there's more than one good enough way to do this. I think I may have misled you on structure vs .mat files having the same names. The .mat files have different names (of course), but the structures are all named the same. Since I'm looping through them, if one of them is different, the code will err. I would say that writing code with extra steps that you're pretty sure you don't need is inefficient. After searching, trying some things, and then trying what you wrote, I realized that I knew a good enough way that would be faster than spending anymore time figuring out a code that saved me some typing. In the end, I think I pretty much did option one in your answer at the top except in a loop, didnt' I?
Stephen23
Stephen23 am 27 Mai 2020
Bearbeitet: Stephen23 am 27 Mai 2020
"I think I may have misled you .... but the structures are all named the same"
That is okay, the main thing is, that we get there in the end :)
"Since I'm looping through them, if one of them is different, the code will err"
Not with the code that you have shown here, it would just continue and use the previous loop iteration's data. But you can easily write it in a more robust way (by load-ing into an output variable, e.g. T):
D = 'absolute/relative path to where the files are saved';
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
T = load(S(k).name);
S(k).one = T.structurename.fieldone;
end
Note that if the filenames contain numbers then your code could easily load them (and concatenate them) in an unexpected order. You should also read about array preallocation:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by