add each iteration of for loop to a table

6 次查看(过去 30 天)
Hi,
I have the following code;
f=dir('*.csv');
for i=1:length(f)
x=readtable(f(i).name)
y=[sum(x.LogicRate100),sum(x.LogicRate125),sum(x.LogicRate150),sum(x.LogicRate170),sum(x.LogicPower50),sum(x.LogicPower66),sum(x.LogicPower75),sum(x.LogicPower80)]
y=array2table(y)
writetable(y,['out' num2str(i) '.csv']);
end
It reads each file in the and sums the 8 collumns to produce a vairale (y).
I want the output to be a table or array that each row contains the output of each iteration. Currently, I am just saving each row.

采纳的回答

KSSV
KSSV 2021-8-4
编辑:KSSV 2021-8-4
f=dir('*.csv');
T = zeros(length(f),[]) ;
for i=1:length(f)
x=readtable(f(i).name)
y=[sum(x.LogicRate100),sum(x.LogicRate125),sum(x.LogicRate150),sum(x.LogicRate170),sum(x.LogicPower50),sum(x.LogicPower66),sum(x.LogicPower75),sum(x.LogicPower80)]
y=array2table(y)
T(i,:) = table2array(y) ;
end
writematrix(T,'test.csv')
  4 个评论
Patrick Lonergan
Patrick Lonergan 2021-8-4
Do you suggest just to use an array in the sum line?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by