How can I make it so this for loop with function adds new elements to the table each iteration instead of rewriting them?
6 次查看(过去 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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!