Add header to dat file

I have a problem with adding header to dat file. I have multiple dat files each one has matrix like this form
2 3 5 6 2 4 2 8 2 5 7 8 I want the dat file to be in the form in a for loop.
x y z s
2 3 5 6
2 4 2 8
2 5 7 8
Thank you

回答(1 个)

Stephen23
Stephen23 2015-6-15
编辑:Stephen23 2015-6-16
Although it is not clear what your data source is (are you reading from a file, or from variables in MATLAB's memory?), you could try adjusting the formatting strings until you get the desired result.
X = [2 3 5 6; 2 4 2 8; 2 5 7 8];
fmt = sprintf('%s\n',repmat( ' %u',1,size(X,2)));
fid = fopen('temp.dat','wt');
fprintf(fid,' x y z s\n');
fprintf(fid,fmt,X.');
fclose(fid);
which generates this textfile:
x y z s
2 3 5 6
2 4 2 8
2 5 7 8
And for dealing with multiple files, read this:

2 个评论

Thank you very much for your help Is there a way to insert it in a for loop so that the I can record many files with different values of the matrix and keeping the same header
Of course you can do this in a loop: read the link that I gave in my answer to know how, it gives several methods for this.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by