Inserting date/time in an array

조회 수: 23 (최근 30일)
Swasti Khuntia
Swasti Khuntia 2012년 6월 26일
Is it possible to insert date/time in an array? I mean, can I insert dates of a month and time (seconds)?
For e.g., I want to insert
01.04.1987 14:00:00
01.04.1987 14:00:01
.
.
.
.
.
.
.
.
31.04.1987 13:59:59
  댓글 수: 2
Thomas
Thomas 2012년 6월 26일
Every 1 second would be an array 30days*24hrs*60min*60sec=2592000 long..
Do you really want it every second?
Swasti Khuntia
Swasti Khuntia 2012년 6월 27일
Yes, Thomas it's needed !!!!!

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

채택된 답변

Jan
Jan 2012년 6월 28일
편집: Jan 2012년 6월 28일
start = '01.04.1987 14:00:00';
stop = '01.04.1987 15:00:00';
fmt = 'dd.mm.yyyy HH:MM:SS';
startSec = round(datenum(start, fmt) * 86400);
stopSec = round(datenum(stop, fmt) * 86400);
period = (startSec:stopSec) / 86400;
periodStr = datestr(period, fmt);

추가 답변 (2개)

Thomas
Thomas 2012년 6월 28일
편집: Thomas 2012년 6월 28일
Well here is the code
start={'01.04.1987 14:00:00'};
start1={'01.04.1987 14:00:01'};
% you can put your end value here if your memory will allow for it
stop={'01.04.1987 15:00:00'}; % I have kept this 1hr from the start
new_start=datenum(start,'dd.mm.yyyy HH:MM:SS');
new_start1=datenum(start1,'dd.mm.yyyy HH:MM:SS');
new_stop=datenum(stop,'dd.mm.yyyy HH:MM:SS');
difference=new_start1-new_start;
out=new_start:difference:new_stop;
output_array=datestr(out,'dd.mm.yyyy HH:MM:SS');

tlawren
tlawren 2012년 6월 26일
Yes, you can insert dates and times into arrays. To get the format you want, you will likely need to use a strings and cell arrays though. A quick example to get that format is the following.
c = clock;
cs = sprintf('%0.2i.%0.2i.%0.4i %0.2i:%0.2i:%0.2i\n', ...
c(2), c(3), c(1), c(4), c(5), round(c(6)));
With the cs variable on hand, you can then put it almost wherever you want. If you are dealing with numerical data and formatting isn't a big concern, you can also just concatenate c onto your numerical data arrays and then call it a day.
  댓글 수: 2
Swasti Khuntia
Swasti Khuntia 2012년 6월 27일
Thanks Lawren for the solution. But, "clock" gives the current date and time. How to insert an older time frame, i.e., April, 1987??
tlawren
tlawren 2012년 6월 28일
You can actually use the same code I have above, but instead of getting c from clock, you can define c yourself. For example, to get 10:32:55 on April 1, 1987, do the following.
c = [1987,4,1,10,32,55];

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

카테고리

Help CenterFile Exchange에서 Clocks and Timers에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by