How to perform function whos output is a struct in a loop?

1 ビュー (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2023 年 1 月 25 日
編集済み: Stephen23 2023 年 1 月 25 日
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
for k=length(files):-1:1
how can i get the output of the function to populate subsequent rows of the GPdata struct?

回答 (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));
S = [C{:}]
If you really want to use complex, obfuscated approaches involving indexing into a structure array, see this thread:


Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by