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 个)
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 个评论
Hani Abulkhair
2017-2-1
Stephen23
2017-2-1
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!