Add header to dat file
Show older comments
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
Answers (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 Comments
Hani Abulkhair
on 1 Feb 2017
Stephen23
on 1 Feb 2017
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.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!