matlab omiting milliseconds from datenum - matlab

Let
x=7.369030000162731e+05
x is a matlab date and it is equal to
27.07.2017 00:00:01.406
I want to remove the milliseconds from it (ie. .406)
To do this I convert it to datestr with 'dd.mm.yyyy HH:MM:SS' format
and then again to datenum
datenum(datestr(x,'dd.mm.yyyy HH:MM:SS'))
Is there a simpler way to do this.

If you want the manual approach:
y = floor(x*86400)/86400;
because serial date numbers are measured in days, and 86400 is the number of seconds in a day.

Here's a somewhat simpler way that converts x to a date vector, floors all the elements (which only affects the seconds value in index 6), then converts it back to a serial date number:
x = datenum(floor(datevec(x)));

Related

Convert dd.MM.yyyy HH:mm:ss to seconds using MATLAB

I work with csv and must subtract 2 cells with the following date format dd.MM.yyyy HH:mm:ss. I need to extract only the seconds.
Timestamp is a column and I attempted:
data1 = Timestamp(1) ;
data2 = Timstamp(2) ; % returns error
class(data1); % returns cell
data1 - data2 % returns error
How can I convert the cells into a number which I can subtract?
You can index into your cell to extract the date string. Then you can use datenum to convert it to "decimal days since 1st January 0000". Then it's a simple matter of subtracting your starting date (convert that using datenum as well) and changing from decimal days to seconds:
tmp = your_cell{1}; % e.g. 26.11.2020 00:00:00, is a string
tmp_date = datenum(tmp); % MATLAB datenum, decimal days.
tmp_date2 = datenum(your_cell{2});
no_start = tmp_date2-tmp_date; % remove starting date
time_sec = no_start*(24*60*60); % change decimal days to seconds
Or as a oneliner:
(datenum(your_cell{2})-datenum(your_cell{1}))*(24*60*60)

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 to implement linear interpolation in Matlab - Coordinates and dates

I have a CSV file contains data of Hurricane location coordinates.
I'm new to Matlab so I'm not sure how to treat correctly date and hour cells, especially when they are displayed unconventionally.
I need to apply linear interpolation so I can get the date for every 30 minutes.
Let's assume you read the data in as numerical values
Now you have some matrix like so:
data = [20130928 0 21.1 50.0
20130928 600 22.2 50.3
20130928 1200 23.3 50.6
20130928 1800 24.2 50.6];
To convert the first two columns to datetime values, we could do this:
% Concatenate first two columns, including making all times 4 digits by 0 padding
fulltime = [num2str(data(:,1)), num2str(data(:,2), '%.4u')]
% Use datetime to convert (cell) times to dates with given format
dates = datetime(cellstr(fulltime),'inputformat', 'yyyyMMddHHmm');
>> dates = 28-Sep-2013 00:00:00
28-Sep-2013 06:00:00
28-Sep-2013 12:00:00
28-Sep-2013 18:00:00
Now we can easily interpolate. First create an array of times we want to use:
% Data value every 30 mins
interpdates = dates(1):hours(0.5):dates(end)
Then use interp1
interpolateddata = interp1(dates, data(:,3:4), interpdates);
>> interpolateddata = 21.1000 50.0000
21.1917 50.0250
21.2833 50.0500
21.3750 50.0750
...
24.1250 50.6000
24.2000 50.6000

How to move a date conversion to the end of the end of the Quarter?

I'm trying to convert a date in matlab to the end of the quarter after getting data in the quarterly format.
For a date in the format Year-Quarter
>> date1='2014Q1';
>> datetime(date1,'InputFormat','yyyyQQ','Format','MM/dd/yyyy')
ans =
01/01/2014
I would like to make this output 3/31/2014 as that would be the date at the end of the quarter.
Simply create a datetime object for the second quarter of the year, then go back one day in time by subtracting one day from the beginning of the second quarter so you get the end of the first quarter. In that case, your date should become '2014Q2' to denote the second quarter of the year.
>> date1 = '2014Q2'; %// Change - note Q2, not Q1
>> datetime(date1,'InputFormat','yyyyQQ','Format','MM/dd/yyyy') - days(1)
ans =
03/31/2014
The function days gives you a datetimeobject that's the equivalent of 1 day, and so subtracting this from your datetime object that starts in the second quarter will give you the end of the first quarter.

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);