Converting to matlab time - matlab

I have strings of the format '15:10:21' for time, and I also know the date, which is in the format 2011-08-05.
What's the best way to obtain matlab time (in days since 1900) out of this data?

Use datenum:
>> num = datenum('2011-08-05 15:10:21')
num =
7.3472e+05
>> datestr(num)
ans =
05-Aug-2011 15:10:21
The "matlab time" is actually days since the 0th of January, in the year 0:
>> datestr(0)
ans =
00-Jan-0000

Related

Why do 4 days in the output of datetime() correspond to 1 month and 4 days with datestr() in Matlab?

I am running into the following problem in Matlab 2019a.
>> datestr(datetime('20190927','InputFormat','yyyyMMdd')-datetime('20190923','InputFormat','yyyyMMdd'),'yyyymmdd')
ans =
'00000104'
Why is the answer 1 month 4 days? Shouldn't it be zero month 4 days?
EDIT:
I realize that arithmetic on datetime arrays create duration arrays. But I don't know how to set format of duration arrays after doing arithmetic on datetime arrays or how to retrieve the duration data in a format free way.
Datetime arithmetic is what I need. So I am asking the question so I can do datetime arithmetic without issue.
d = datetime('20190927','InputFormat','yyyyMMdd')-datetime('20190923','InputFormat','yyyyMMdd')
returns:
d =
duration
96:00:00
That is, 96 hours. Converting this to a date string results in January 4th. If you start counting hours from midnight on the year 0, 96 hours later you end up on January 4th. There's no 0th month.
What you can do is this:
d = duration(d,'Format','dd:hh:mm:ss')
which returns:
d =
duration
04:00:00:00
You can also do:
d = days(d)
which returns:
d =
4

How can I make datetime in multiples of 5 in MATLAB?

I have a date
time1 = '03-Apr-2004 00:15:00'
aTime = datenum(time1)
time2 = datestr(addtodate(aTime, -53.1*60, 'minute'))
31-Mar-2004 19:09:00
But I want time2 to be in the nearest multiple of 5 minutes always. How can I do this (either by adding or multiplying some factor)?
Expected answer: 31-Mar-2004 19:10:00
Basically, in my entire dataset, I want these values to be always 5,10,15,20,...,55 etc. in the minute section.
What you have, in fact, is a datenum, not datetime. You could, for example, first convert it to datevec, then round the minutes, then convert back to datenum:
>> dv = datevec(time2);
>> dv(5) = round(dv(5) / 5) * 5;
>> datestr(datenum(dv))
ans =
31-Mar-2004 19:10:00
If it is possible that seconds are non-zero, you can also set them to zero:
dv(6) = 0;

Date Comparsion in Matlab

I would like to check whether the date I pass in into a function comes after 01/02 or 01/08. How do i do that in Matlab? This comparsion does is independent of the year.
Need some guidance on this.
Btw, how do u calculate the difference between dates irregardless of year?
Use datenum, with a format specifier if you want to be explicit:
D0 = datenum('01/02','mm/dd'); % current year is inferred (irrelevant)
D1 = datenum('01/08'); % 'mm/dd' is inferred
This allows you to test with regular comparison operators:
>> D = datenum('01/03')
>> D > D0
ans =
1
>> D > D1
ans =
0

MATLAB - plotting vector against a cell array

I want to do something like
scatter(timesRefined, upProb)
where timesRefined is a cell array in which each entry is a string corresponding to a time moment, such as 8:32:21.122 and upProb is simply a vector of numbers with same length as cell array. What is the most convenient way to do this?
You can convert your timesRefined cell to a numeric representation of date with datenum
>> timesRefined = {'8:32:21.122','9:30:54.123'};
>> datenum(timesRefined)
ans =
734869.355800023
734869.396459757
The resulting number expresses a date as days from the epoch. Since you are not concerned with days, just time, and provided your observations are contained within one day, you can simply take the fractional part of the datenum output:
>> datestr(mod(datenum(timesRefined),1))
ans =
8:32 AM
9:30 AM
and do scater(mod(datenum(timesRefined),1),upProb)
EDIT:
As pointed out by Pursuit, you can use the result of datenum directly as your x values and use datetick('x','HH:MM:SS.FFF')
strsplit from the Matlab file exchange should help. If all values are numeric, you'll get a matrix back.
timestr = '8:32:21.122';
timenum = strsplit(timestr,':');
convmat = [60*60; 60; 1];
time_in_seconds = sum(timenum .* convmat);

Subtract months from a given date in Matlab

I need to subtract 21 months from a given date.
My solution as given below only takes me to the first month of the given year :(
[a,b,c]= datevec(date);
b= b-21;
datestr(datenum(a,b,c)) %--> 11-Jan-2011 (WRONG).
I want the answer to be 11-June-2009.
Go via date numbers rather than date vectors and use addtodate:
>> d = datenum(date);
>> e = addtodate(d, -21, 'month');
>> datestr(e)
ans =
11-Jun-2009