Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

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

1 vue (au cours des 30 derniers jours)
H R
H R le 23 Sep 2015
Clôturé : MATLAB Answer Bot le 20 Août 2021
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.

Réponses (1)

Star Strider
Star Strider le 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

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by