changing header to csv using writetable
Ältere Kommentare anzeigen
I have a 2D array like this: dates and data
19790101 7839.4 29.4 17.3 1.6 51.8 13.8 5.2
19790102 8046.4 30.1 17.5 0.1 45.2 16.2 4.9
19790103 9018.2 27.6 13.5 0 40.9 14.3 5.2
19790104 5359.4 21.8 14.4 0.2 61.5 8 4.9
19790105 8145.9 26.4 13.6 0.7 52.2 11.2 3.8
...
I saved it into a csv file as
T = array2table(d);
T.Properties.VariableNames(1:8) = {'a','b','c','d','e','f','g','h'};
writetable(T,'myfile.csv','WriteVariableNames',0)
Since writetable doesn't allow headers using ( ) or other characters, I used 'a' 'b'...
What I need id to change that arbitrary header for another one like
Date Rad(Wm-2) Tmx(C) Tmn(C) Pre(mm) Rhd(%) Vpd(hPa) Wp10(ms-1)
I didn't do this directly using csvwrite because it writes the output of the first column in scientific notation
Thank you
Akzeptierte Antwort
Weitere Antworten (1)
Ameer Hamza
am 16 Nov. 2020
Try something like this
T = [ ...
19790101 7839.4 29.4 17.3 1.6 51.8 13.8 5.2
19790102 8046.4 30.1 17.5 0.1 45.2 16.2 4.9
19790103 9018.2 27.6 13.5 0 40.9 14.3 5.2
19790104 5359.4 21.8 14.4 0.2 61.5 8 4.9
19790105 8145.9 26.4 13.6 0.7 52.2 11.2 3.8 ];
fid = fopen('myfile.csv', 'w');
fprintf(fid, 'Date Rad(Wm-2) Tmx(C) Tmn(C) Pre(mm) Rhd(%%) Vpd(hPa) Wp10(ms-1)\n');
fclose(fid);
writematrix(T, 'myfile.csv', 'WriteMode', 'append')
5 Kommentare
Simon Lind
am 16 Nov. 2020
Ameer Hamza
am 16 Nov. 2020
Try this
T = [ ...
19790101 7839.4 29.4 17.3 1.6 51.8 13.8 5.2
19790102 8046.4 30.1 17.5 0.1 45.2 16.2 4.9
19790103 9018.2 27.6 13.5 0 40.9 14.3 5.2
19790104 5359.4 21.8 14.4 0.2 61.5 8 4.9
19790105 8145.9 26.4 13.6 0.7 52.2 11.2 3.8 ];
fid = fopen('myfile.csv', 'w');
fprintf(fid, 'Date Rad(Wm-2) Tmx(C) Tmn(C) Pre(mm) Rhd(%%) Vpd(hPa) Wp10(ms-1)\n');
fclose(fid);
dlmwrite('myfile.csv', T, '-append')
Simon Lind
am 16 Nov. 2020
Ameer Hamza
am 17 Nov. 2020
You may try fprintf with custom formatting
T = [ ...
19790101 7839.4 29.4 17.3 1.6 51.8 13.8 5.2
19790102 8046.4 30.1 17.5 0.1 45.2 16.2 4.9
19790103 9018.2 27.6 13.5 0 40.9 14.3 5.2
19790104 5359.4 21.8 14.4 0.2 61.5 8 4.9
19790105 8145.9 26.4 13.6 0.7 52.2 11.2 3.8 ];
fid = fopen('myfile.csv', 'w');
fprintf(fid, 'Date Rad(Wm-2) Tmx(C) Tmn(C) Pre(mm) Rhd(%%) Vpd(hPa) Wp10(ms-1)\n');
fprintf(fid, '%8.0f %4.1f %2.1f %2.1f %2.1f %2.1f %2.1f %2.1f \n', T.');
fclose(fid);
Simon Lind
am 17 Nov. 2020
Kategorien
Mehr zu Text Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!