I have two matrices A and B as given below:
A = [1
2
3
4
5]
B = [10 11 12 13
15 16 17 18
17 12 15 13
20 21 22 17
40 41 32 33]
and I would like to output it to a text file in the form given below with column headers as shown:
Desired text output
A B B B B
1 10 11 12 13
2 15 16 17 18
3 17 12 15 13
4 20 21 22 17
5 40 41 32 33
Reproducible code
A = [1; 2; 3; 4; 5];
B = [10, 11, 12, 13;
15, 16, 17, 18;
17, 12, 15, 13;
20, 21, 22, 17;
40, 41, 32, 33;];
ALL = [A B];
ALL_cell = mat2cell(ALL, ones(size(ALL,1),1), size(ALL,2));
fID = fopen('output.dat','w');
f = @(x) fprintf(fID,'%s\n',sprintf('%f\t',x));
cellfun(f,ALL_cell);
fclose(fID);
How to insert the column headers as shown above with MATLAB? Sometimes, the columns in B could be over 100, as an example I have given only 4.