How to merge cell arrays with different dimensions?

I have two cell arrays. The first one (A) has 1x14 cells, and the second (B) has 1x7 cells. I would like to create an array (or a table) that would consists of two arrays in vertical merging. The commands e.g. table(A,B) etc. shows me error about the dimensions of each one.
Could you please help me ?

2 commentaires

What would be the expected size and datatype of the "two arrays in vertical merging" ?
Are you looking for an output table with 14 variables, with the first row being the content of A, and the first 7 entries of the second row being the content of B, and with the second 7 entries of the second row being some kind of padding (such as empty cells) ?
Yes , Just like that you mentioned @Walter Roberson

Connectez-vous pour commenter.

 Réponse acceptée

system('copy file1.txt+file2.txt+file3 OUTPUT_Merged_file.txt');
This is the solution

Plus de réponses (1)

T = table(A{:});
temp = cell(1, length(A));
temp(1:length(B)) = B;
T = [T; temp];
Note that using [] between a table and a cell array has the effect of inserting the content of the cell array as additional rows in the table.

5 commentaires

ok, I would like to ask something. the first cell starts with "#" character. As a result command line shows me the error
Invalid parameter name: # .
Caused by:
You might have intended to create a one-row table with the character vector '# ' as one of its
variables. To store text data in a table, use a string array or a cell array of character vectors
rather than character arrays. Alternatively, create a cell array with one row, and convert that to
a table using CELL2TABLE.
I tried CELL2TABLE and shows me again an error:
Error using cell2table (line 24)
C must be a 2-D cell array.
What can I do?
temp = cell(2, length(A));
temp(1,:) = A;
temp(2,1:length(B)) = B;
T = cell2table(temp);
Ivan Mich
Ivan Mich le 2 Fév 2022
Modifié(e) : Ivan Mich le 3 Fév 2022
Ok thank you. I have one last question. If I want to add from the 3rd line (after the B matrix I discussed) 3 columns which command should I use?
I have this matrix with three columns (see attached image)
I used these command
filename1= 'testdata2.txt'
[d1,tex]= importdata(filename1);
C1=d1.data(:,2);
C2=d1.data(:,3);
C3=d1.data(:,1);
data=[num2cell(C1) num2cell(C2) num2cell(C3)]
temp = cell(2, length(A));
temp(1,:) = A;
temp(2,1:length(B)) = B;
temp(3,1:length(data)) = data
T = cell2table(temp);
but no use (command line shows me: Unable to perform assignment because the size of the left side is 1-by-19 and the size of the right
side is 19-by-3.)
Could you please help me?
let me explain, I mean I would like to have one output file with:
  • the first line of A (1x14)
  • the second line of B (1x7)
  • the third line of data (19x3)
These three bulltes must be merged vertically, in the order I mentioned above, in the final aoutput file.
could you please help me to make it?
system('copy file1.txt+file2.txt+file3 OUTPUT_Merged_file.txt');
This is the solution

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by