How to use str2num function to extract a specific year from the array?

조회 수: 5(최근 30일)
Hi!
I have a column matrix consists of string arrays. It's a huge time matrix and I am just showing a part of it. It contains data from year 2001 to 2019.
'2003-09-30T20:32:34Z'
'2003-09-30T20:47:34Z'
'2003-09-30T21:02:34Z'
'2003-09-30T21:17:34Z'
'2003-09-30T21:32:34Z'
'2003-09-30T21:47:34Z'
'2003-09-30T22:02:34Z'
'2003-09-30T22:17:34Z'
'2003-09-30T22:32:34Z'
'2003-09-30T22:47:34Z'
'2003-09-30T23:02:34Z'
'2003-09-30T23:17:34Z'
'2003-09-30T23:32:34Z'
'2003-09-30T23:47:34Z'
'2004-05-23T13:45:00Z'
'2004-05-23T14:00:00Z'
'2004-05-23T14:15:00Z'
'2004-05-23T14:30:00Z'
'2004-05-23T14:45:00Z'
'2004-05-23T15:00:00Z'
'2005-05-23T15:15:00Z'
'2005-05-23T15:30:00Z'
'2005-05-23T15:45:00Z'
'2005-05-23T16:00:00Z'
'2005-05-23T16:15:00Z'
'2005-05-23T16:30:00Z'
'2006-05-23T16:45:00Z'
'2006-05-23T17:00:00Z'
'2006-05-23T17:15:00Z'
'2006-05-23T17:30:00Z'
'2006-05-23T17:45:00Z'
'2007-05-23T18:00:00Z'
If I want to extract only the data from 2003, how can I select those specific rows of 2003? I personally want to use this command -
str2num(time{i}(1:4))==2003
But if there is a better alternative, I am happy to use it! Thank you so much.

채택된 답변

Star Strider
Star Strider 2022년 7월 8일
C = {'2003-09-30T20:32:34Z'
'2003-09-30T20:47:34Z'
'2003-09-30T21:02:34Z'
'2003-09-30T21:17:34Z'
'2003-09-30T21:32:34Z'
'2003-09-30T21:47:34Z'
'2003-09-30T22:02:34Z'
'2003-09-30T22:17:34Z'
'2003-09-30T22:32:34Z'
'2003-09-30T22:47:34Z'
'2003-09-30T23:02:34Z'
'2003-09-30T23:17:34Z'
'2003-09-30T23:32:34Z'
'2003-09-30T23:47:34Z'
'2004-05-23T13:45:00Z'
'2004-05-23T14:00:00Z'
'2004-05-23T14:15:00Z'
'2004-05-23T14:30:00Z'
'2004-05-23T14:45:00Z'
'2004-05-23T15:00:00Z'
'2005-05-23T15:15:00Z'
'2005-05-23T15:30:00Z'
'2005-05-23T15:45:00Z'
'2005-05-23T16:00:00Z'
'2005-05-23T16:15:00Z'
'2005-05-23T16:30:00Z'
'2006-05-23T16:45:00Z'
'2006-05-23T17:00:00Z'
'2006-05-23T17:15:00Z'
'2006-05-23T17:30:00Z'
'2006-05-23T17:45:00Z'
'2007-05-23T18:00:00Z'};
DT = datetime(C, 'InputFormat','yyyy-MM-dd''T''HH:mm:ss''Z''', 'TimeZone','UTC') % Create 'datetime' Array
DT = 32×1 datetime array
30-Sep-2003 20:32:34 30-Sep-2003 20:47:34 30-Sep-2003 21:02:34 30-Sep-2003 21:17:34 30-Sep-2003 21:32:34 30-Sep-2003 21:47:34 30-Sep-2003 22:02:34 30-Sep-2003 22:17:34 30-Sep-2003 22:32:34 30-Sep-2003 22:47:34 30-Sep-2003 23:02:34 30-Sep-2003 23:17:34 30-Sep-2003 23:32:34 30-Sep-2003 23:47:34 23-May-2004 13:45:00 23-May-2004 14:00:00 23-May-2004 14:15:00 23-May-2004 14:30:00 23-May-2004 14:45:00 23-May-2004 15:00:00 23-May-2005 15:15:00 23-May-2005 15:30:00 23-May-2005 15:45:00 23-May-2005 16:00:00 23-May-2005 16:15:00 23-May-2005 16:30:00 23-May-2006 16:45:00 23-May-2006 17:00:00 23-May-2006 17:15:00 23-May-2006 17:30:00
Y2003 = year(DT) == 2003; % Logical Vector
Result = DT(Y2003)
Result = 14×1 datetime array
30-Sep-2003 20:32:34 30-Sep-2003 20:47:34 30-Sep-2003 21:02:34 30-Sep-2003 21:17:34 30-Sep-2003 21:32:34 30-Sep-2003 21:47:34 30-Sep-2003 22:02:34 30-Sep-2003 22:17:34 30-Sep-2003 22:32:34 30-Sep-2003 22:47:34 30-Sep-2003 23:02:34 30-Sep-2003 23:17:34 30-Sep-2003 23:32:34 30-Sep-2003 23:47:34
For table ‘T1’, the addressing would be:
Result = T1(Y2003,:)
to capture all the variables in ‘T1’ for those years.
.

추가 답변(0개)

범주

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by