Add header to dat file
Mostrar comentarios más antiguos
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
Respuestas (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 comentarios
Hani Abulkhair
el 1 de Feb. de 2017
Stephen23
el 1 de Feb. de 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.
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!