loop to read files
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!