merge multiple csv or xls files with different row and columns

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 commentaires

"I have multiple csv files, with different rows and columns and i need to merge them in one single csv file."
What exactly does "merge" mean? Perhaps vertical concatenation, or horizontal concatenation, or some kind of JOIN operation... we cannot guess what you intend, if you do not either explain it clearly of provide a sufficiently precise example.
Sorry, I'm quite new to matlab.
I meant horizontal concatenation.
Basically i have some csv files 4x3, 1x1, 3x2 and so on, and i have to obtain only one file with every table horizontal concatenated.
That'll be extremely awkward as a .csv file, in particular, if you don't have same number of variables/fields every record; csv files only work well with regular records. It can be made to work (in a fashion) otherwise, but not pleasant to deal with.
How are you going to know what belongs to what after you've done this? -- it would seem you'll have various "stuff" all mixed up together.
Better show us an example of what you've got and what you think you want and how it would be intended to be used...
Isn't this something that WRITEMATRIX/WRITECELL/etal's RANGE option should be able to handle?
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.

Connectez-vous pour commenter.

Réponses (1)

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 commentaires

Sorry, I haven't understood. Lets say i have 3 files named like this. File0.csv, File1.csv and File2.csv What have i to write? I haven't understood your code sorry

AllCell = {};
for filenum = 0:2
filename = "File" + filenum + ".csv";
NewCell = readcell(filename);
newrows = size(NewCell,1); newcols = size(NewCell,2);
AllCell(1:newrows, end:end+newcols) = NewCell;
end
writecell(AllCell, "Combined.csv");
Massimo
Massimo le 26 Mai 2023
Modifié(e) : Massimo le 26 Mai 2023
I have just tried, but it doesn't work. It gives me this error:
"Index in position 2 is invalid. Array indices must be positive integers or logical values."
I forgot to say my tables contains different informations. Some file contains only numbers, other contains only words.
AllCell(1:newrows, end+1:end+newcols) = NewCell;
Massimo
Massimo le 26 Mai 2023
Modifié(e) : Massimo le 26 Mai 2023
nothing to do. I have assigned your code to a push button in matlab app designer, but it gives me this error command window:
"Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn."
Thank you however for your time
edit: Sorry, I'm starting to think there is some problem with matlab version. Just to try i have used the same app designer file on my personal pc, where i have matlab 2022b and everything works!
Now i continue to investigate, but you might have solved me an enormous problem. I really really thank you again.
% 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{:}
end
A = 4×3 cell array
{[0.3191]} {[0.0762]} {[0.4635]} {[0.0297]} {[0.7799]} {[0.7663]} {[0.7568]} {[0.2999]} {[0.0310]} {[0.9638]} {[0.1558]} {[0.1982]}
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 4-by-3.
The output cell array still has to be rectangular even though it is a cell array; you could only catenate the disparate cell arrays by augmenting the sizes to all have the same number of rows -- if the first is the tallest, then you can do it by adding to the next one read; if one of those has more rows than a prior one, then have to augment the accumulator array to its size.
You could encapsulate the returned cell arrays from readcell into a cell array as I did to create the sample data, but when you do that, writecell doesn't write each cell in turn after catenating, it will just expand them all in a row...
% 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...
% 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}

Connectez-vous pour commenter.

Produits

Version

R2019b

Question posée :

le 25 Mai 2023

Commenté :

le 26 Mai 2023

Community Treasure Hunt

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

Start Hunting!

Translated by