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

 채택된 답변

Star Strider
Star Strider 2020년 11월 16일
편집: Star Strider 2020년 11월 16일

0 개 추천

In R2016b, the variable names must be valid MATLAB variable names. (That changed in R2020a, so what you want to do would be permitted.)
The other problem you are having is that the dates in column #1 need to be a datetime array.
This does everything except use the variable names you want:
M = [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 ];
T1 = array2table(M);
DT = datetime(num2str(M(:,1)), 'InputFormat','yyyyMMdd');
T1.M1 = DT
producing:
T1 =
5×8 table
M1 M2 M3 M4 M5 M6 M7 M8
___________ ______ ____ ____ ___ ____ ____ ___
01-Jan-1979 7839.4 29.4 17.3 1.6 51.8 13.8 5.2
02-Jan-1979 8046.4 30.1 17.5 0.1 45.2 16.2 4.9
03-Jan-1979 9018.2 27.6 13.5 0 40.9 14.3 5.2
04-Jan-1979 5359.4 21.8 14.4 0.2 61.5 8 4.9
05-Jan-1979 8145.9 26.4 13.6 0.7 52.2 11.2 3.8
Change the variable names to versions of what you have that will work in R2016a. Even if you change them to what you want in the file, they will be changed to valid MATLAB variable names when you import it. Your only option is to upgrade to R2020a or later to use the variable names you want to use.
You can then use writetable with ‘T1’ (or whatever you wish to name it in your code).
EDIT — (16 Nov 2020 at 20:40)
To have the datetime column in the original format, add the 'Format' name-value pair to the datetime call in the ‘DT’ assignment:
DT = datetime(num2str(M(:,1)), 'InputFormat','yyyyMMdd', 'Format','yyyyMMdd');
The rest of the code is unchanged.
M1 M2 M3 M4 M5 M6 M7 M8
________ ______ ____ ____ ___ ____ ____ ___
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
.

댓글 수: 2

Simon Lind
Simon Lind 2020년 11월 17일
thank you very much for your help
Star Strider
Star Strider 2020년 11월 17일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 16일

0 개 추천

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

Simon Lind
Simon Lind 2020년 11월 16일
Thank you. I forgot to mention that I'm using R2016b version
any alternative to writematrix?
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
Simon Lind 2020년 11월 16일
thank you. I tried this option but dlmwrite writes the first column in scientific notation
1.98E+07
instead of
19790101
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
Simon Lind 2020년 11월 17일
thank you very much for your help

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2020년 11월 16일

댓글:

2020년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by