Creation timeSeries using a datetime variable

45 views (last 30 days)
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,

Accepted Answer

Adam Barber
Adam Barber on 12 Aug 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 Comment
Cedric Mariage
Cedric Mariage on 13 Aug 2015
Thanks for your answer , I manage to improve the speed of the code so it's working now.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!