Effacer les filtres
Effacer les filtres

Creation timeSeries using a datetime variable

34 vues (au cours des 30 derniers jours)
Cedric Mariage
Cedric Mariage le 10 Août 2015
Modifié(e) : per isakson le 15 Août 2015
Hi- I'm new to matlab world and I do have some issue dealing with the timeseries
I have two arrays
  • One time array(name time) which is a string cell array like this "dd-MM-yyy HH:mm:SS"
  • One array which represents my value for each time
I found a solution but it’s quite slow
% transformed my time array in datetime
Time2 = datetime(time,'InputFormat','dd-MM-yyyy HH:mm:SS')
%transforme my datetime array in date string
Time3= datestr(Time2)
%Create the timeseries
Ts = timeseries ( data,Time3)
This code is working but it's very slow is that possible to improve this?
Thanks in advance
Regards,

Réponse acceptée

Adam Barber
Adam Barber le 12 Août 2015
Hi Cedric,
If you already have your dates in a cell array string format, you should not need to convert to datetime in order to create a timeseries. For example, see the code below
%%Generate 100 random dates with a specified string format
datenumbers = rand(100,1)*now;
datenumbers = sort(datenumbers); % put them in increasing order
dateStrings = datestr(datenumbers, 'dd-mm-yyyy HH:MM:SS');
%%At this point "dateStrings" should be just like the data you have
Ts = timeseries(data, dateStrings);
Currently you cannot create a timeseries with "datetimes", but using a date string should work. If this is still slow for you, you may want to post either your data and full code or the results of running the profiler on the code (hit the "Run and Time" button).
Hope that helps,
-Adam
  1 commentaire
Cedric Mariage
Cedric Mariage le 13 Août 2015
Thanks for your answer , I manage to improve the speed of the code so it's working now.

Connectez-vous pour commenter.

Plus de réponses (0)

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