Adding to an already existing matlab struct in a for loop

19 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

maryam al labbad
maryam al labbad il 13 Dic 2020
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
  3 Commenti
Mathieu NOE
Mathieu NOE il 14 Dic 2020
and then do :
for K = 1 : numfiles
File_Path {K} = fullfile(File_Struct(K).folder, File_Struct(K).name); % not sure it is used anywhere
thisfile = filenames{K};
x1 = importdata( filenames1{K} );
% U=x.data(:,1);
% Y=x.data(:,2);
% P=x.data(:,3);
Data.C(1).UU=(x1.data(:,1))';
Data.C(1).YY=(x1.data(:,2))';
Data.C(1).PP=(x1.data(:,3))';
Data.C(2).UU=(x2.data(:,1))';
Data.C(2).YY=(x2.data(:,2))';
Data.C(2).PP=(x2.data(:,3))';
save(fullfile(File_Struct(K).folder, sprintf('Data_%d.mat',K)),'Data')
end

Accedi per commentare.

Più risposte (1)

Mathieu NOE
Mathieu NOE il 13 Dic 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 Commenti
maryam al labbad
maryam al labbad il 13 Dic 2020
Thanks for the replay, please diregard the :
Data.C(1).TimeSeries=U';
Data.C(1).TimeSeries=P';
Data.C(1).TimeSeries=Y';
I am using different code but listed this since that one is long. it is something like:
Data.C(1).UU.TimeSeries=U';
Data.C(1).PP.TimeSeries=P';
Data.C(1).YY.TimeSeries=Y';
basically, considering the 40 data sets I have, I need to store every 2 in one file to end up with 20 files in total.
So, I am not sure if I should load the first data set within the loop and then save the second data set on the file and so on? if so would you please show an example of how could this be done?
thanks
Mathieu NOE
Mathieu NOE il 13 Dic 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')

Accedi per commentare.

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by