MATLAB: Adding to an already existing matlab struct in a for loop

add datafor loopMATLAB

Hi all,
I am proccessing 20 data sets with a for loop.
myFolder = ''
File_Struct = dir(fullfile(myFolder, '*.ap'))
filenames = {File_Struct.name};
numfiles = length(filenames);
for K = 1 : numfiles
File_Path {K} = fullfile(File_Struct(K).folder, File_Struct(K).name);
thisfile = filenames{K};
x = importdata( thisfile );
U=x.data(:,1);
Y=x.data(:,2);
P=x.data(:,3);
Data.C(1).TimeSeries=U';
Data.C(1).TimeSeries=P';
Data.C(1).TimeSeries=Y';
save(fullfile(File_Struct(K).folder, sprintf('Data_%d.mat',K)),'Data')
end
So this code provides me with 20 .mat files named as Data_1,…..,Data_20.
I want to repeat same above procedure on another 20 sets of data, but instead of saving them seperatly, I want matlab to load Data_1, and add the Data_1 from second batch and so on. see below:
myFolder2 = ''
File_Struct2 = dir(fullfile(myFolder2, '*.ap'))
filenames2 = {File_Struct2.name};
numfiles2 = length(filenames2);
for K = 1 : numfiles
File_Path2 {K} = fullfile(File_Struct2(K).folder, File_Struct2(K).name);
thisfile2 = filenames2{K};
x = importdata( thisfile2 );
U=x.data(:,1);
Y=x.data(:,2);
P=x.data(:,3);
Data.C(2).TimeSeries=U';
Data.C(2).TimeSeries=P';
Data.C(2).TimeSeries=Y';
save(fullfile(File_Struct(K).folder, sprintf('Data_%d.mat',K)),'Data')
end
I am not sure how to let matlab load the Data file saved at step 1 and let matlab save on it.
ANy help is apprecaited

Best Answer

Actually, I ran the first code in all of the 40 data sets. So I need a way mostly to combine the data automatically in struct array (1) and (2).
Every 20 .mat files are saved in seperate folder. That is why I was thinking to load the 1_data.mat from folder 1, and somehow load data_1.mat from folder 2 and combine. Thanks