Inserting date/time in an array

25 次查看(过去 30 天)
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