フィルターのクリア

Numerate lines to a text file

2 ビュー (過去 30 日間)
George
George 2013 年 10 月 14 日
回答済み: Azzi Abdelmalek 2013 年 10 月 14 日
i write this matrix to a .txt file and i would like to numerate the lines, for example :
1 5 1 6
2 1 2 8
3 1 3 15
....
50 2 1 10
51 2 2 20
52 2 3 13
....
Please will someone help me?

回答 (3 件)

sixwwwwww
sixwwwwww 2013 年 10 月 14 日
編集済み: sixwwwwww 2013 年 10 月 14 日
Here is an example for your problem:
A = rand(100, 3);
num = 1:100;
B = [num' A];
disp(B)
dlmwrite('filename.txt', B, '\t');
In case you don't know the size of matrix you want to numerate then you can do like this:
[m, n] = size(YourMatrix);
num = 1:m;
I hope it helps

Vivek Selvam
Vivek Selvam 2013 年 10 月 14 日
編集済み: Vivek Selvam 2013 年 10 月 14 日
Hi George
A more rudimentary C like approach is as follows:
[rows cols] = size(A);
formatSpecRowNum = '%3.2d ';
formatSpec = '%5.4f ';
formatSpecNewline = '\n';
fileID = fopen('myFile.txt','w+');
for i = 1:rows
fprintf(fileID,formatSpecRowNum,i);
for j = 1:cols
fprintf(fileID,formatSpec,A(i,j));
end
fprintf(fileID,formatSpecNewline);
end
fclose(fileID);

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 14 日
If A is your nx3 array
c1=1:size(A,1)
A=[c1; A']
fid=fopen('filename.txt','w')
fprintf(fid, '%d %d %d %d \r\n',A);
fclose(fid)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by