loop to read files

2 次查看(过去 30 天)
alpedhuez
alpedhuez 2020-6-12
I have data_2010.csv, data_2011.csv,... in the same directory. I want to write a loop like
for i=2010:1:2020
(table data_i)= readtable(data_i.csv)
end
Please advise.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-6-12
编辑:Ameer Hamza 2020-6-12
To read all csv files one by one
files = dir('*.csv');
table_data = cell(1, numel(files));
for i=1:numel(files)
filename = files(i).name;
table_data{i} = readtable(filename);
end
To only read these specific files
range = 2010:1:2020;
table_data = cell(1, numel(range));
for i = 1:numel(range)
filename = sprintf('data_%d.csv', range(i));
table_data{i} = readtable(filename);
end

更多回答(0 个)

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by