Inserting date/time in an array

15 vues (au cours des 30 derniers jours)
Swasti Khuntia
Swasti Khuntia le 26 Juin 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 commentaires
Thomas
Thomas le 26 Juin 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 le 27 Juin 2012
Yes, Thomas it's needed !!!!!

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 28 Juin 2012
Modifié(e) : Jan le 28 Juin 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);

Plus de réponses (2)

Thomas
Thomas le 28 Juin 2012
Modifié(e) : Thomas le 28 Juin 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 le 26 Juin 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 commentaires
Swasti Khuntia
Swasti Khuntia le 27 Juin 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 le 28 Juin 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];

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by