Read and manipulate multiple .csv files
220 views (last 30 days)
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 on 9 Mar 2020
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.
data = readmatrix(files(i).name); % or csvread(files(i).name)
% process data
writematrix(processed_data,txt_filename); % save to txt file