How can I make it so this for loop with function adds new elements to the table each iteration instead of rewriting them?

조회 수: 35 (최근 30일)
Hi everybody,
Wish you can help me with this one:
I am trying to create a for loop which puts a number of files through a batch filing process (function inside the for loop). As a result the for loop should run the function on all the files and create a table with some data from them, the thing is I cant figure out how to make it so the new data is added into the table in each iteration instead of the table being rewritten.
Code below:
clear
[file,folder]=uigetfile('*.xls;*.xlsx;*.csv');
filename=fullfile(folder,file);
Nbook = readtable(filename);
d = dir('*.wcp');
Num_files = height(d);
Names = cell(0);
for nf = 1:Num_files
Fname = fullfile(d(nf).name);
Names{nf} = Fname;
Names = Names.';
end
for nn = 1:height(Names)
DataFile = char(Names(nn));
DataTable = BatchProcess(DataFile,Nbook)
end
Here is the BatchProcess function, I think the problem is only on the last line of code which creates the table:
data = import_wcp(DataFile);
EPSPdata = data.S{1,1};
num_samp = size(EPSPdata);
Interval = data.t_interval;
t= 0:Interval:(num_samp-1)*Interval;
t = t'*1000;
[~,startI] = min(abs(t-13));
[~,endI] = min(abs(t-35));
window = startI:endI;
smoothfactor = 2;
smData = EPSPdata;
numTrace = size(EPSPdata,2);
for n = 1:numTrace
smData(:,n,1) = smooth(EPSPdata(:,n,1), smoothfactor);
end
[peak, ~] = min(smData(window,:));
peak = peak.';
PeakData = peak;
CONTROLeq = (Nbook.CADO(1)-1);
CADOeq = (Nbook.CADO_DPCPX(1)-1);
DPCPXeq = (Nbook.CADO_DPCPX_NBQX(1)-1);
NBQXeq = (length(data.rec_index)-1);
CONTROLData = smData(:,CONTROLeq);
CADOData = smData(:,CADOeq);
DPCPXData = smData(:, DPCPXeq);
NBQXData = smData(:,NBQXeq);
[~,startG] = min(abs(t-7));
[~,endG] = min(abs(t-35));
windowG = startG:endG;
subplot(2,4,1)
CONTROLrecording = plot(t(windowG), CONTROLData(windowG,:));
ylim([-0.8 0.2]);
xlabel 'Time (ms)'
ylabel 'Voltage (mv)'
title('i')
subplot(2,4,2)
CADOrecording = plot(t(windowG), CADOData(windowG,:));
ylim([-0.8 0.2]);
title('ii')
subplot(2,4,3)
DPCPXrecording = plot(t(windowG), DPCPXData(windowG,:));
ylim([-0.8 0.2]);
title('iii')
subplot(2,4,4)
NBQXrecording = plot(t(windowG), NBQXData(windowG,:));
ylim([-0.8 0.2]);
title('iv')
PlotT = 0:10:10*((length(data.rec_index)-1));
PlotT = PlotT'/60;
subplot(2,4,[5,6,7,8])
TimeCoursePlot = scatter(PlotT, PeakData);
xlabel 'Time (min)'
ylabel '1st fEPSP Amplitude (mv)'
xline([PlotT(CONTROLeq) PlotT(CADOeq) PlotT(DPCPXeq) PlotT(NBQXeq)],'-',{'i','ii','iii', 'iv'},'LabelOrientation','horizontal');
DataTable = table(convertCharsToStrings(DataFile), PeakData(CONTROLeq), PeakData(CADOeq), PeakData(DPCPXeq), PeakData(NBQXeq), 'VariableNames', {'Filename','Control Peak', 'CADO Peak', 'DPCPX Peak', 'NBQX Peak'});
end

답변 (1개)

Vinesh Katewa
Vinesh Katewa 2021년 7월 13일
Hi,
I believe you are trying to get a new row for your table in each iteration but with this code you are re-writing the DataTable variable and receiving the data of final iteration of the for loop. You need to convert the table into MATLAB 'struct', keep adding data of each iteration into that struct and finally convert the array of struct back into table.
You can achieve this using something like this:
DataTable = []
for nn = 1:height(Names)
DataFile = char(Names(nn));
Data = BatchProcess(DataFile,Nbook);
DataTable = [DataTable table2struct(Data)]; % appending data into the struct array
end
DataTable = struct2table(DataTable)
The DataTable at the end of iteration will contain multiple rows with each row corresponding to data from each iteration of the for loop.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by