Inserting date/time in an array

23 visualizaciones (últimos 30 días)
Swasti Khuntia
Swasti Khuntia el 26 de Jun. de 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 comentarios
Thomas
Thomas el 26 de Jun. de 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 el 27 de Jun. de 2012
Yes, Thomas it's needed !!!!!

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 28 de Jun. de 2012
Editada: Jan el 28 de Jun. de 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);

Más respuestas (2)

Thomas
Thomas el 28 de Jun. de 2012
Editada: Thomas el 28 de Jun. de 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 el 26 de Jun. de 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 comentarios
Swasti Khuntia
Swasti Khuntia el 27 de Jun. de 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 el 28 de Jun. de 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];

Iniciar sesión para comentar.

Categorías

Más información sobre Clocks and Timers en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by