Merging CSV files with similar data structure into table
이전 댓글 표시
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
Miyuru K
2016년 8월 22일
편집: Walter Roberson
2016년 8월 22일
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
2016년 8월 23일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!