Azzera filtri
Azzera filtri

Creating a text file to specific format

1 visualizzazione (ultimi 30 giorni)
Damith
Damith il 23 Mag 2014
Commentato: Damith il 26 Mag 2014
Hi,
I have a matrix called data (3652500 x 3). But data matrix dont have the header (i.e. names).
I want to insert column headers as shown below and write the output to a text file inserting 4th column from another matrix called output (3652500 x 1). I need to insert the output matrix in the 4th column of data matrix with 3 spaces between 3rd columns and 4th column. (See the header and sample rows below)
DATE TMAX TMIN RAIN
3000001 -99.9 -99.9 0.6
3000002 -99.9 -99.9 0.5
How can I do it MATLAB.
Any help is appreciated.
Thanks.

Risposta accettata

Image Analyst
Image Analyst il 23 Mag 2014
Modificato: Image Analyst il 24 Mag 2014
With fprintf() you can make it look anyway you want. Have you tried that function?
output = 99*rand(10,4);
output(:,1) = round(output(:,1))
filename = 'deleteme.txt';
fid = fopen(filename, 'wt');
if fid ~= -1
fprintf(fid, 'DATE TMAX TMIN RAIN\n');
for row = 1 : size(output, 1)
fprintf(fid, '%d %.1f %.1f %.1f\n', output(row, :));
end
fclose(fid);
end
  5 Commenti
Image Analyst
Image Analyst il 25 Mag 2014
It's just like any format string that you're used to in most any other language that you've ever used. If you use %.1f, the field width varies according to the size of the number. If you put %6.1f, it will pad out to make it a fixed field width of 6 characters.
Damith
Damith il 26 Mag 2014
Thanks. I figured it out.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by