필터 지우기
필터 지우기

how to create array datetime

조회 수: 3 (최근 30일)
Luca Re
Luca Re 2024년 5월 13일
댓글: Stephen23 2024년 5월 20일
i want datetime in this format :
01/02/2008 501 (day/month/years and time)
i try it :
bbb=datetime(bb,"InputFormat", "dd/MM/yyyy");
but i get error format
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2024년 5월 13일
Based on your graphic, 01/02/2008 501 is actually (month/day/year time)

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

채택된 답변

Voss
Voss 2024년 5월 13일
load matlab_A
A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hh = floor(A(:,4)/100);
mm = mod(A(:,4),100);
ss = zeros(size(A,1),1);
B = [A(:,[1 2 3]) hh mm ss];
D = datetime(B,'Format','dd/MM/yyyy Hmm')
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530
  댓글 수: 6
Luca Re
Luca Re 2024년 5월 20일
ok..
Stephen23
Stephen23 2024년 5월 20일
Note that by supplying the units separately you could minimize the seconds to one single 0 and write less code:
A = load('matlab_A.mat').A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hh = fix(A(:,4)/100);
mm = mod(A(:,4),100);
D = datetime(A(:,1),A(:,2),A(:,3),hh,mm,0, 'Format','dd/MM/yyyy Hmm')
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530

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

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2024년 5월 13일
편집: Cris LaPierre 2024년 5월 13일
You have the correct function, just the wrong syntax. The biggest issue I see with the conversion is that your time appears to be in millitary format, or HHmm.
I therefore think the simplest approach is to convert your array into a string arrary and then use the syntax t = datetime(DateStrings)
load matlab_A.mat
A
A = 1000x4
2008 1 2 501 2008 1 2 502 2008 1 2 503 2008 1 2 504 2008 1 2 505 2008 1 2 506 2008 1 2 507 2008 1 2 508 2008 1 2 509 2008 1 2 510
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Capture the time
D = datetime(num2str(A),'InputFormat',"yyyy M d Hmm",'Format',"dd/MM/yyyy Hmm")
D = 1000x1 datetime array
02/01/2008 501 02/01/2008 502 02/01/2008 503 02/01/2008 504 02/01/2008 505 02/01/2008 506 02/01/2008 507 02/01/2008 508 02/01/2008 509 02/01/2008 510 02/01/2008 511 02/01/2008 512 02/01/2008 513 02/01/2008 514 02/01/2008 515 02/01/2008 516 02/01/2008 517 02/01/2008 518 02/01/2008 519 02/01/2008 520 02/01/2008 521 02/01/2008 522 02/01/2008 523 02/01/2008 524 02/01/2008 525 02/01/2008 526 02/01/2008 527 02/01/2008 528 02/01/2008 529 02/01/2008 530

Luca Re
Luca Re 2024년 5월 13일
편집: Luca Re 2024년 5월 13일
thank for answer
The originally array is
it's a very large array
i try your solution but the PC no longer responded
i use ctr+c to break loop..
i try it:
tic
[A,~]=importdata(bubu);
toc
Elapsed time is 3.253890 seconds.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by