How to perform function whos output is a struct in a loop?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to loop a data extraction program over multiple files in a string array, and place the results in a struct, but i continue getting the error
Subscripted assignment between dissimilar structures
GPdata=struct();
for k=length(files):-1:1
[GPdata(k)]=extractDataV2(files(k));
end
how can i get the output of the function to populate subsequent rows of the GPdata struct?
0 个评论
回答(1 个)
Stephen23
2023-1-25
编辑:Stephen23
2023-1-25
Assuming identical fieldnames, the simplest and most robust solution is to concatenate the structure after the loop:
N = numel(files);
C = cell(1,N);
for k = 1:N
C{k} = extractDataV2(files(k));
end
S = [C{:}]
If you really want to use complex, obfuscated approaches involving indexing into a structure array, see this thread:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!