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

Risposte (1)

Stephen23
Stephen23 il 15 Giu 2015
Modificato: Stephen23 il 16 Giu 2015
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 Commenti

Hani Abulkhair
Hani Abulkhair il 1 Feb 2017
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
Stephen23
Stephen23 il 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.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Tag

Richiesto:

il 15 Giu 2015

Commentato:

il 1 Feb 2017

Community Treasure Hunt

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

Start Hunting!

Translated by