Concatenate same fields in multiple structures in a loop using field names
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2020-5-26
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.
采纳的回答
Stephen23
2020-5-26
编辑:Stephen23
2020-5-26
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 个评论
Stephen23
2020-5-27
编辑:Stephen23
2020-5-27
"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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!