Info

This question is closed. Reopen it to edit or answer.

Write a 1D matrix to a text file such that only 6 numbers printed in each line

1 view (last 30 days)
Hello all, I have a long 1D matrix A for example say: A=A[1,2,3,4,5,6,...,21]; I want to write this matrix in to a tab delimited text file in such a way that not more than 6 numbers is printed in each line in the text file:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21
Could you please help on how to do that in matlab? Thank you.

Answers (1)

Star Strider
Star Strider on 23 Sep 2015
There are probably different ways to do this, but this works:
A = 1:21; % Original Data
A1 = {reshape(A(1:6*fix(length(A)/6)), [], 6); A(6*fix(length(A)/6)+1:end)}; % Create Cell Array
filename = 'NewFile.csv';
dlmwrite(filename, A1{1})
dlmwrite(filename, A1{2}, '-append')
type(filename) % Proof!
1,4,7,10,13,16
2,5,8,11,14,17
3,6,9,12,15,18
19,20,21

Tags

Community Treasure Hunt

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

Start Hunting!