loop to read files

3 views (last 30 days)
alpedhuez
alpedhuez on 12 Jun 2020
Edited: Ameer Hamza on 12 Jun 2020
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.

Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Jun 2020
Edited: Ameer Hamza on 12 Jun 2020
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

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!