append several csv files into one

조회 수: 1 (최근 30일)
Angelavtc
Angelavtc 2020년 1월 20일
댓글: Walter Roberson 2020년 6월 13일
Hello to all!
I am trying to append some csv files into one file using the following code:
csv1 = csvread('2011_basehourlycurves_demand.csv');
csv2 = csvread('2012_basehourlycurves_demand.csv');
allCsv = [csv1;csv2];
csvwrite('combined_csv', allCsv);
However, when I try to call my data it appears the following error:
Error using dlmread
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file (row number 1, field number 1) ==>
Which I guess is because one of my varible/columns is composed by a categorical variable.
How can I fix this error? I tried using textscan, but It was not working.
Thank you in advance!
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 1월 23일
Never use csvread for content that contains text.
Andrew Janke
Andrew Janke 2020년 1월 31일
Are you doing power/electricity market analysis? Always interesting to see another Energy person in the Matlab space!

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

채택된 답변

Ahmed Makki
Ahmed Makki 2020년 6월 13일
편집: Walter Roberson 2020년 6월 13일
I applied the same script that you used to combine 8 csv files and it worked.
t1 = csvread('B155p1.csv');
t2 = csvread('B155p2.csv');
t3 = csvread('B155p3.csv');
t4 = csvread('B155p4.csv');
t5 = csvread('B155p5.csv');
t6 = csvread('B155p6.csv');
t7 = csvread('B155p7.csv');
t8 = csvread('B155p8.csv');
allData = [t1;t2;t3;t4;t5;t6;t7;t8];
csvwrite('combined_155.csv', allData);
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 6월 13일
csvread() will only work for data that is all numeric; the user had categorical data.

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

추가 답변 (1개)

Andrew Janke
Andrew Janke 2020년 1월 31일
If you're using a newer version of Matlab, you probably want to use readtable instead of csvread. Don't forget the file extension for your output file.
t1 = readtable('2011_basehourlycurves_demand.csv');
t2 = readtable('2012_basehourlycurves_demand.csv');
allDAta = [t1;t2];
writetable('combined_csv.csv', allData);

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by