loop to read files

조회 수: 4 (최근 30일)
alpedhuez
alpedhuez 2020년 6월 12일
편집: Ameer Hamza 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개)

카테고리

Help CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by