Inserting date/time in an array

17 views (last 30 days)
Swasti Khuntia
Swasti Khuntia on 26 Jun 2012
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 Comments
Thomas
Thomas on 26 Jun 2012
Every 1 second would be an array 30days*24hrs*60min*60sec=2592000 long..
Do you really want it every second?
Swasti Khuntia
Swasti Khuntia on 27 Jun 2012
Yes, Thomas it's needed !!!!!

Sign in to comment.

Accepted Answer

Jan
Jan on 28 Jun 2012
Edited: Jan on 28 Jun 2012
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);

More Answers (2)

Thomas
Thomas on 28 Jun 2012
Edited: Thomas on 28 Jun 2012
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 on 26 Jun 2012
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 Comments
Swasti Khuntia
Swasti Khuntia on 27 Jun 2012
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 on 28 Jun 2012
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];

Sign in to comment.

Categories

Find more on Dates and Time 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!