I have a matrix with one column in serial datenumber format, how do I select data from a certain time period?

조회 수: 1 (최근 30일)
I have a large dataset where the first column is filled with serial datenumbers. I want to use these to select the rows of data for time periods of interest. For example, the first row in this data set (2) corresponds to the 29th May 2019 and runs til the end of July. I want to extract, for example, data from June only. How do I do this? I know I could convert the column to datetime and then select the rows of interest from there, but is there a simpler way?
Untitled.png
  댓글 수: 4
Louise Wilson
Louise Wilson 2019년 10월 25일
I could just convert the column to datetime I guess, how would I do this? I planned to keep it as is as I'll use it later, but I guess it would be easier to change it back...
Adam Danz
Adam Danz 2019년 10월 25일
If there's no need to be able to read the dates, you can just use indexing as Stephen Cobeldick explained in his answer. If you need to read the dates, you can convert them and using indexing from datetime format as well.

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

채택된 답변

Stephen23
Stephen23 2019년 10월 25일
편집: Stephen23 2019년 10월 25일
Serial date numbers are, as their name implies, just numbers. So it is very easy to use logical comparisons, just like you would for comparing any other numbers.
First we generate some fake data (because you did not provide any):
>> N = 23;
>> D = linspace(datenum(2019,5,29),datenum(2019,7,31),N);
>> M = [D(:),randi(99,N,8)]
M =
737574.00000 63.00000 70.00000 22.00000 74.00000 1.00000 27.00000 91.00000 91.00000
737576.86364 74.00000 36.00000 6.00000 57.00000 28.00000 43.00000 41.00000 72.00000
737579.72727 7.00000 99.00000 22.00000 56.00000 18.00000 95.00000 67.00000 79.00000
737582.59091 70.00000 68.00000 33.00000 73.00000 93.00000 87.00000 99.00000 44.00000
737585.45455 78.00000 57.00000 99.00000 21.00000 42.00000 27.00000 13.00000 22.00000
737588.31818 19.00000 95.00000 79.00000 4.00000 43.00000 67.00000 15.00000 10.00000
737591.18182 70.00000 33.00000 75.00000 67.00000 97.00000 46.00000 64.00000 89.00000
737594.04545 42.00000 48.00000 14.00000 18.00000 61.00000 65.00000 26.00000 48.00000
737596.90909 62.00000 7.00000 68.00000 18.00000 6.00000 76.00000 72.00000 90.00000
737599.77273 39.00000 77.00000 59.00000 79.00000 83.00000 77.00000 98.00000 48.00000
737602.63636 90.00000 93.00000 84.00000 4.00000 84.00000 66.00000 42.00000 21.00000
737605.50000 74.00000 13.00000 66.00000 12.00000 1.00000 74.00000 64.00000 48.00000
737608.36364 15.00000 20.00000 34.00000 71.00000 4.00000 1.00000 25.00000 54.00000
737611.22727 45.00000 65.00000 83.00000 26.00000 19.00000 47.00000 5.00000 53.00000
737614.09091 15.00000 25.00000 9.00000 94.00000 68.00000 45.00000 57.00000 52.00000
737616.95455 50.00000 84.00000 52.00000 47.00000 52.00000 82.00000 55.00000 16.00000
737619.81818 85.00000 33.00000 39.00000 97.00000 70.00000 41.00000 65.00000 41.00000
737622.68182 44.00000 88.00000 7.00000 16.00000 7.00000 47.00000 7.00000 8.00000
737625.54545 33.00000 33.00000 44.00000 60.00000 79.00000 60.00000 17.00000 58.00000
737628.40909 21.00000 29.00000 78.00000 63.00000 5.00000 69.00000 49.00000 68.00000
737631.27273 37.00000 43.00000 53.00000 72.00000 68.00000 33.00000 27.00000 39.00000
737634.13636 58.00000 56.00000 96.00000 27.00000 61.00000 89.00000 33.00000 56.00000
737637.00000 85.00000 58.00000 32.00000 22.00000 99.00000 91.00000 16.00000 80.00000
Then we can use basic MATLAB indexing to select only June's data:
>> idb = M(:,1) >= datenum(2019,6,1); % start of June
>> ide = M(:,1) < datenum(2019,7,1); % start of July
>> out = M(idb&ide,:)
out =
737579.72727 7.00000 99.00000 22.00000 56.00000 18.00000 95.00000 67.00000 79.00000
737582.59091 70.00000 68.00000 33.00000 73.00000 93.00000 87.00000 99.00000 44.00000
737585.45455 78.00000 57.00000 99.00000 21.00000 42.00000 27.00000 13.00000 22.00000
737588.31818 19.00000 95.00000 79.00000 4.00000 43.00000 67.00000 15.00000 10.00000
737591.18182 70.00000 33.00000 75.00000 67.00000 97.00000 46.00000 64.00000 89.00000
737594.04545 42.00000 48.00000 14.00000 18.00000 61.00000 65.00000 26.00000 48.00000
737596.90909 62.00000 7.00000 68.00000 18.00000 6.00000 76.00000 72.00000 90.00000
737599.77273 39.00000 77.00000 59.00000 79.00000 83.00000 77.00000 98.00000 48.00000
737602.63636 90.00000 93.00000 84.00000 4.00000 84.00000 66.00000 42.00000 21.00000
737605.50000 74.00000 13.00000 66.00000 12.00000 1.00000 74.00000 64.00000 48.00000
And checking the first column to see that it only contains dates from June:
>> datestr(out(:,1))
ans =
03-Jun-2019 17:27:16
06-Jun-2019 14:10:54
09-Jun-2019 10:54:32
12-Jun-2019 07:38:10
15-Jun-2019 04:21:49
18-Jun-2019 01:05:27
20-Jun-2019 21:49:05
23-Jun-2019 18:32:43
26-Jun-2019 15:16:21
29-Jun-2019 12:00:00

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by