Clear Filters
Clear Filters

How to combine multiple CSV files with Headers?

27 views (last 30 days)
Hello all
I have hundreds of CSV files all containing data from different datalogs, but each csv file has the same format like this:
Time Variable1 Variable2 Variable3 VariableN Date
.1 12 5 3 9 date
Now I want to combine all of these CSV files into one massive CSV to import into a database for data processing. Each CSV has the exact same header as shown above. The CSVs are all different lengths of time, some might be just a few hundred lines and other might be thousands of lines. I've tried using functions like readtable, but they seem to combine the CSV files so messy and all the headers are smushed into one column in excel. How can I combine these CSVs?
  1 Comment
Voss
Voss on 22 Apr 2022
Can you upload (using the paperclip icon) one or more of those .csv files? Particularly ones that readtable seems to mess up. Sometimes you have to go through some trial-and-error with the readtable options before it does what you want with a given file.

Sign in to comment.

Answers (1)

Li Ding
Li Ding on 23 Apr 2022
Edited: Li Ding on 23 Apr 2022
Use a for loop to repeat reading each one and combing them one by one. These two commands will be inside the loop:
newSheet = readcell('fileTitle'); %use sprintf() to create the file title, or use uigetfile
combined = [combined; newSheet(2:end,:)] %need to initialize combined = [] before the loop

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!