Converting datenum to datetime when using writetable

조회 수: 7 (최근 30일)
Joel
Joel 2023년 4월 13일
댓글: Stephen23 2023년 4월 13일
Hi all
I want to export some datetime data to excel. I dont know how.
In addition, for certain years, there are no values. I show this by replacing values by zero
A = datenum(t0_pos1); %values 2021
B = datenum(t0_pos12); %values 2022
if isempty(B)==1
B=zeros(1,1);
end
C = [];
C(:,1) = A;
C(:,2) = B;
borjan=array2table(C,'VariableNames',{'2021','2022'});
writecell(G2,'asd2.xlsx','Sheet',1,'Range','H2')
writetable(borjan,'asd2.xlsx','Sheet',1,'Range','H3')
I got numbers into excel, but I want the datenum to be converted to datetime (yy-mm-dd-hh).
  댓글 수: 1
Stephen23
Stephen23 2023년 4월 13일
Do not convert perfectly good DATETIMEs to deprecated DATENUMs!
You can easily merge two DATETIME arrays using the EMPTY method of the appropriate class:
A = datetime(2023,1,1:9).'
A = 9×1 datetime array
01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023
B = datetime(2023,1,[])
B = 0×0 empty datetime array
C = datetime.empty;
C(1:numel(A),1) = A;
C(1:numel(B),2) = B
C = 9×2 datetime array
01-Jan-2023 NaT 02-Jan-2023 NaT 03-Jan-2023 NaT 04-Jan-2023 NaT 05-Jan-2023 NaT 06-Jan-2023 NaT 07-Jan-2023 NaT 08-Jan-2023 NaT 09-Jan-2023 NaT
and then WRITEMATRIX as usual. Avoid deprecated DATENUM, DATESTR, and DATEVEC.

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 4월 13일
You convert your datetimes to datenums, and then write those numbers to Excel, which is what you are seeing.
Don't convert your datetimes to datenums. Then, you can use the format cells option in Excel to set the date format it gets displayed with.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by