Adding to an already existing matlab struct in a for loop
Show older comments
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
Accepted Answer
More Answers (1)
Mathieu NOE
on 13 Dec 2020
hello
first , I am not sure what you are doing here :
Data.C(1).TimeSeries=U';
Data.C(1).TimeSeries=P';
Data.C(1).TimeSeries=Y';
the second line will overwritte the first and so forth
for the second part of the question, you can create the Data cell aray inside the loop
and then , outside the for loop, save the entire Data cell array in one shot
for
....
2 Comments
maryam al labbad
on 13 Dec 2020
Mathieu NOE
on 13 Dec 2020
sory , my last part of my reply was broken
once you are done with the for loop , and Data is filled with all the required data ,
why not simply save in one file :
save(fullfile(File_Struct(1).folder, 'Data_all.mat','Data')
Categories
Find more on Multirate Signal Processing 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!