필터 지우기
필터 지우기

merge multiple csv or xls files with different row and columns

조회 수: 34 (최근 30일)
Massimo
Massimo 2023년 5월 25일
댓글: Walter Roberson 2023년 5월 26일
Hello guys, i need help to do what describe in title. I have multiple csv files, with different rows and columns and i need to merge them in one single csv file. I don't know at all what to do.
I have created csv files, but if you think it's better dealing with xls files, i can also create them as xls.
However, whatever type they are, at the end i need to have just one single file (csv or xls).
Thanks
  댓글 수: 5
Stephen23
Stephen23 2023년 5월 26일
Isn't this something that WRITEMATRIX/WRITECELL/etal's RANGE option should be able to handle?
Walter Roberson
Walter Roberson 2023년 5월 26일
Not when you are writing to a csv file. To add columns to a csv file, the (internal) code would have to read the entire file, insert the new values, and write out the result. As opposed to xls files which can be updated in-place.

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

답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 25일
readcell() each file. Carefully concatenate them together
newrows = size(NewCell,1); newcols = size(NewCell,2);
AllCell(1:newrows, end:end+newcols) = NewCell;
afterwards writecell(AllCell)
  댓글 수: 8
dpb
dpb 2023년 5월 26일
% simulate some data returned from readcell
c{1}=num2cell(rand(4,3));
c{2}=num2cell(rand(1));
c{3}=num2cell(rand(3,2))
c = 1×3 cell array
{4×3 cell} {1×1 cell} {3×2 cell}
Assume we catenate the return from readcell as above
writecell(c, "Combined.csv") % write it out
Error using writecell
Nested cell arrays are not supported.
I don't see any magic elixir here that would work without more effort. One could write a spreadsheet file by writing each file separately to the next available columns with the 'range' argument, but would have to keep running total of number columns written already.
It still doesn't make much sense to me to put such data together this way...
Walter Roberson
Walter Roberson 2023년 5월 26일
% simulate some data returned from readcell
c{1}=num2cell(rand(4,3));
c{2}=num2cell(rand(1));
c{3}=num2cell(rand(3,2))
c = 1×3 cell array
{4×3 cell} {1×1 cell} {3×2 cell}
A={};
for i=1:3
[nr,nc]=size(c{i}); % readcell returns a nxm cell array
A(1:nr,end+1:end+nc)=c{i}
end
A = 4×3 cell array
{[0.7031]} {[0.1522]} {[0.8339]} {[0.0438]} {[0.9413]} {[0.5937]} {[0.2233]} {[0.5167]} {[0.3666]} {[0.9291]} {[0.7885]} {[0.6691]}
A = 4×4 cell array
{[0.7031]} {[0.1522]} {[0.8339]} {[ 0.3683]} {[0.0438]} {[0.9413]} {[0.5937]} {0×0 double} {[0.2233]} {[0.5167]} {[0.3666]} {0×0 double} {[0.9291]} {[0.7885]} {[0.6691]} {0×0 double}
A = 4×6 cell array
{[0.7031]} {[0.1522]} {[0.8339]} {[ 0.3683]} {[ 0.1050]} {[ 0.4291]} {[0.0438]} {[0.9413]} {[0.5937]} {0×0 double} {[ 0.4317]} {[ 0.6401]} {[0.2233]} {[0.5167]} {[0.3666]} {0×0 double} {[ 0.4467]} {[ 0.6084]} {[0.9291]} {[0.7885]} {[0.6691]} {0×0 double} {0×0 double} {0×0 double}

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

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by