Fill the column with pre-defined time intervals
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi,
I need to fill the column A below with pre-defined time intervals for each day. For example, Jan 1 1998
1/1/1998 0:00
1/1/1998 3:00
1/1/1998 4:00
1/1/1998 6:00
1/1/1998 9:00
1/1/1998 10:00
1/1/1998 12:00
1/1/1998 15:00
1/1/1998 16:00
1/1/1998 18:00
1/1/1998 21:00
1/1/1998 22:00
1/2/1998 0:00
A=
1/1/1998 0:00
1/1/1998 4:00
1/1/1998 6:00
1/1/1998 10:00
1/1/1998 12:00
1/1/1998 16:00
1/1/1998 18:00
1/1/1998 22:00
1/2/1998 0:00
1/2/1998 4:00
1/2/1998 6:00
1/2/1998 12:00
1/2/1998 18:00
1/3/1998 0:00
1/15/1998 18:00
1/16/1998 0:00
1/16/1998 6:00
1/16/1998 12:00
1/16/1998 18:00
1/17/1998 0:00
1/17/1998 6:00
1/17/1998 12:00
1/17/1998 18:00
1/18/1998 0:00
1/18/1998 6:00
1/18/1998 12:00
1/18/1998 18:00
1/19/1998 0:00
1/19/1998 6:00
1/19/1998 12:00
1/19/1998 18:00
1/20/1998 0:00
1/20/1998 6:00
1/20/1998 12:00
1/20/1998 18:00
1/21/1998 0:00
1/21/1998 6:00
1/21/1998 12:00
1/21/1998 18:00
1/22/1998 0:00
1/22/1998 6:00
1/22/1998 12:00
1/22/1998 18:00
1/23/1998 0:00
1/23/1998 6:00
1/22/1998 10:00
1/22/1998 12:00
1/22/1998 18:00
1/22/1998 22:00
1/23/1998 0:00
Is there a way to this MATLAB?
Thanks in advance.
채택된 답변
Star Strider
2015년 2월 20일
One way to do it:
D0 = datenum([1998 01 01]);
DT = 1/24; % Days/Hour
DM = eomday(1998,01); % Days In January
DV = D0 + cumsum([0; ones(24*DM,1)*DT]);
Jan1998 = datestr(DV(1:end-1), 'mm/dd/yyyy HH:MM');
Result = [Jan1998(1:5,:); Jan1998(end-4:end,:)] % Check Result
produces:
Result =
01/01/1998 00:00
01/01/1998 01:00
01/01/1998 02:00
01/01/1998 03:00
01/01/1998 04:00
01/31/1998 19:00
01/31/1998 20:00
01/31/1998 21:00
01/31/1998 22:00
01/31/1998 23:00
The ‘Result’ variable just looks at the first and last 5 entries.
댓글 수: 12
Thanks again for your quick reply. Your outcome is not the result I want. Sorry may be my exaplanation is not clear.
Actually, I want to insert 0,3,6,9,12,15,18,21 time steps for each day if these time steps are not available in Column A. But also I need to keep the time steps for example, 4 and 10, in column A.
A=
1/1/1998 0:00
1/1/1998 4:00
1/1/1998 6:00
1/1/1998 10:00
1/1/1998 12:00
1/1/1998 16:00
1/1/1998 18:00
1/1/1998 22:00
1/2/1998 0:00
I don’t have sufficient energy tonight to copy your ‘A’ matrix, since I would have to put quotes around each row, and either create them as a cell array of strings, or add leading zeros to get all the columns to have the same width to create a character array.
Change the appropriate line to:
Jan1998 = datestr(DV(1:3:end-1), 'mm/dd/yyyy HH:MM');
to get every third hour, then concatenate it with ‘A’ (they have to have the appropriate column dimensions for that to work) and use the unique function to create your output array, something like this:
A = unique([Jan1998; A]);
That sorts them as well, so you would have non-repeating values that combine both arrays.
I have created the sample "A" matrix with quotes.
Now, I need to read first row and end row of "A" matrix and then remove rows which are other than 0,3,6,9,12,15,18,21 from A and if A matrix does not have the above time intervals (0,3,6,9,12,15,18,21) for a given date then include it between the date ranges of first row and end row.
Hope this helps.
A=
'1/1/1998 0:00'
'1/1/1998 4:00'
'1/1/1998 6:00'
'1/1/1998 10:00'
'1/1/1998 12:00'
'1/1/1998 16:00'
'1/1/1998 18:00'
'1/1/1998 22:00'
'1/2/1998 0:00'
'1/2/1998 4:00'
'1/2/1998 6:00'
'1/2/1998 12:00'
'1/2/1998 18:00'
'1/3/1998 0:00'
I need leading zeros on everything, but once I supplied them, my idea works:
D0 = datenum([1998 01 01]);
DT = 1/24; % Days/Hour
% DM = eomday(1998,01); % Days In January
DM = 2;
DV = D0 + cumsum([0; ones(24*DM,1)*DT]);
Jan1998 = datestr(DV(1:3:end-1), 'mm/dd/yyyy HH:MM');
% Result = [Jan1998(1:5,:); Jan1998(end-4:end,:)] % Check Result
A= ['01/01/1998 00:00'
'01/01/1998 04:00'
'01/01/1998 06:00'
'01/01/1998 10:00'
'01/01/1998 12:00'
'01/01/1998 16:00'
'01/01/1998 18:00'
'01/01/1998 22:00'
'01/02/1998 00:00'
'01/02/1998 04:00'
'01/02/1998 06:00'
'01/02/1998 12:00'
'01/02/1998 18:00'
'01/03/1998 00:00'];
A = unique([Jan1998; A], 'rows');
produces:
A =
01/01/1998 00:00
01/01/1998 03:00
01/01/1998 04:00
01/01/1998 06:00
01/01/1998 09:00
01/01/1998 10:00
01/01/1998 12:00
01/01/1998 15:00
01/01/1998 16:00
01/01/1998 18:00
01/01/1998 21:00
01/01/1998 22:00
01/02/1998 00:00
01/02/1998 03:00
01/02/1998 04:00
01/02/1998 06:00
01/02/1998 09:00
01/02/1998 12:00
01/02/1998 15:00
01/02/1998 18:00
01/02/1998 21:00
01/03/1998 00:00
that I believe is what you want.
You will have to adapt it to your particular application, supplying ‘A’ with leading zeros where necessary, so each numeric field is of the appropriate length. Otherwise, it won’t work.
Thanks. How can I change my "A" matrix to double. It's a char now.
str2double(A) won't work?
@ Sad Grad Student. Nope it doesn't work.
@Damith — I would use the datenum function to convert them to double. My ‘DV’ variable already does that, so you can use it with the datenum output of your dates, and the unique call in my code should work the same. (There is always the potential problem of slight mismatches due to approximation errors in floating-point operations, but with the dates as you have defined them, that should not be a problem. The datenum representations of the hours should be unambiguous.)
My code then becomes:
D0 = datenum([1998 01 01]);
DT = 1/24; % Days/Hour
% DM = eomday(1998,01); % Days In January
DM = 2;
DV = D0 + cumsum([0; ones(24*DM,1)*DT]);
DV = DV(1:3:end-1);
A= ['01/01/1998 00:00'
'01/01/1998 04:00'
'01/01/1998 06:00'
'01/01/1998 10:00'
'01/01/1998 12:00'
'01/01/1998 16:00'
'01/01/1998 18:00'
'01/01/1998 22:00'
'01/02/1998 00:00'
'01/02/1998 04:00'
'01/02/1998 06:00'
'01/02/1998 12:00'
'01/02/1998 18:00'
'01/03/1998 00:00'];
A = unique([DV; datenum(A, 'mm/dd/yyyy HH:MM')]);
Result = datestr(A, 'mm/dd/yyyy HH:MM')
with the same ‘Result’ as before:
Result =
01/01/1998 00:00
01/01/1998 03:00
01/01/1998 04:00
01/01/1998 06:00
01/01/1998 09:00
01/01/1998 10:00
01/01/1998 12:00
01/01/1998 15:00
01/01/1998 16:00
01/01/1998 18:00
01/01/1998 21:00
01/01/1998 22:00
01/02/1998 00:00
01/02/1998 03:00
01/02/1998 04:00
01/02/1998 06:00
01/02/1998 09:00
01/02/1998 12:00
01/02/1998 15:00
01/02/1998 18:00
01/02/1998 21:00
01/03/1998 00:00
Star,
Thanks. I realized that there are lat,long coordinates attached to time.
Can you find me a way to do this,
https://www.mathworks.com/matlabcentral/answers/179517-read-excel-file-and-fill-rows
Thanks again.
My pleasure.
I considered that and I downloaded and tried to deal with the times and other data in that file, in the context of this question, but gave up. I wasn’t able to do it.
No worries. Thanks.
Star,
I have simplified the table now. Can you help me a way to this. I think it should be easy now.
https://www.mathworks.com/matlabcentral/answers/180349-select-rows-from-table-and-interpolate
Thanks in advance.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
