Merging CSV files with similar data structure into table
Show older comments
Merging CSV files into table
I am trying to find a way to read each csv file with read table and merge into a big table without “heavy” coding. I am having problems merging the tables due to a somewhat non-uniform data structure and wonder if the process of making the table uniform can be done in Matlab (instead of copy pasting in excel).
Questions:
How to I check if variable exist in csv file?
How do I concatenate specific variables to existing table?
Problem: The original data are in csv files and organized as follows:
There is one csv file for each year
The data structure is not the same in each csv file. Eg file2001.csv may have variables “height” only; file 2005.csv may have variable “height” and “colour” and 20015.csv may have “height”, “size” and “colour”.
Generic Code:
Table2001=readtable(‘file2001.csv); % contains subset of variables
Table2005=readtable(‘file205.csv); % contains subset of variables
Table2015=readtable(‘file2015.csv); % contains all variables)
Create big table
T = Table2015; % starting table as it contains all variables
Check if “height” is a variable in Table2001;
if yes, concatenate column below T.height
if no, add column of NAN below T.height
continue process for all variables and files
2 Comments
Miyuru K
on 22 Aug 2016
Edited: Walter Roberson
on 22 Aug 2016
Do you want to store the year as well? Would your final table look like this?
Year height colour size
2015 1 1 1
...
2001 1 NAN NAN
Do you want to uniquely identify each row in the table using a key variable? If not, just load them into arrays.
Björn
on 23 Aug 2016
Accepted Answer
More Answers (0)
Categories
Find more on Tables 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!