Read and manipulate multiple .csv files

Hi,
I have multiple *.csv files which contains almost similar data. All the csv files have random and dissimilar file-names. Is there a way to load them, read them one by one and work with the data?
To elaborate, my end goal is to read one csv file, change certain data in that file and save it as a .txt file and continue this for all the files. And I have 1000+ csv files with almost nothing in common. I'm okay with the manipulation part but I need some help with loading and reading all csv files from a certain directory, one by one.
Thanks in advance!

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 9일

1 개 추천

You can use dir to read the name of all files with csv extensions.
files = dir('*.csv');
Then iterate over the files struct to read each file.
for i=1:length(files)
data = readmatrix(files(i).name); % or csvread(files(i).name)
% process data
writematrix(processed_data,txt_filename); % save to txt file
end

댓글 수: 3

Jake
Jake 2020년 3월 9일
Thank you!
metumetu
metumetu 2020년 3월 12일
편집: metumetu 2020년 3월 12일
@Ameer Hamza Do we need convert txt files to csv? and writematrix doesnt work on 2018a. what can we do alternatively?
Ameer Hamza
Ameer Hamza 2020년 3월 13일
metumetu, you do not need to conver txt to csv. You can directly read data from txt file.
writematrix will is not available in R2018a. You can use readtable, textscan, dlmread or fscanf to read data from txt file. The exact solution will depend on the formatting of your txt file.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2020년 3월 9일

댓글:

2020년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by