Exporting to text file - adding a header

6 ビュー (過去 30 日間)
Kayleigh
Kayleigh 2013 年 11 月 29 日
コメント済み: Kayleigh 2013 年 11 月 29 日
Hi!
I was just wondering what the best way to add a header would be to a text file I am exporting using the following command provided by a kind member of another forum:
x = [1:5];
y = rand(1,5);
fileID = fopen('yourfile.txt','w');
for i = 1:length(x)
fprintf(fileID,'%s\t%d\t%d\t%f\n', 'Test', x(i),x(i),y(i));
end
fclose(fileID);
This creates a text file with the format:
Test 1 1 0.655741
Test 2 2 0.035712
Test 3 3 0.849129
Test 4 4 0.933993
Test 5 5 0.678735
And I just need to be able to add a text header to the very first line of the .txt file consisting of a few words:
Word1 Word2 Word3
Test 1 1 0.655741
Test 2 2 0.035712
Test 3 3 0.849129
Test 4 4 0.933993
Test 5 5 0.678735
Thanks for any help!
- K

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 29 日
x = [1:5];
y = rand(1,5);
fileID = fopen('yourfile.txt','w');
fprintf(fileID,'your text\n')
for i = 1:length(x)
fprintf(fileID,'%s\t%d\t%d\t%f\n', 'Test', x(i),x(i),y(i));
end
fclose(fileID);
  1 件のコメント
Kayleigh
Kayleigh 2013 年 11 月 29 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOther Formats についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by