Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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.

回答(1 个)

Star Strider
Star Strider 2015-9-23
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

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by