split a double into parts - matlab - matlab

I have a date vector in form
20140117
20130325
20130530
etc.
There are 5,000,000 lines in the double vector.
How can I transfer it to a datevector recognized by matlab?
I don't like changing it to string and putting the parts in separately. It takes too long!
Please Help!

a combination of fix and mod let's you extract the digits you want:
%Matrix Columns YY,MM,DD,hh,mm,ss
[mod(fix(x/10000),100000),mod(fix(x/100),100),mod(x,100),zeros(size(x,1),3)]
%datenum
datenum(mod(fix(x/10000),10000),mod(fix(x/100),100),mod(x,100))

If you really want to avoid casting to string then back to number then you can use this method:
D = [20140117; 20130325; 20130530];
YY = fix( D./10000 ) ;
MM = fix( (D-YY.*10000) /100 ) ;
DD = fix( (D-YY.*10000-MM.*100 ) );
DateInMatlabformat = datenum( YY , MM , DD ) ;
You can package that in a one liner if you want, but basically what it does is:
Divide by 10000 to get the year in the variable YY
Remove this part from your original date ((D-YY.*10000)), then divide by 100 to get the month.
remove all of that, you obtain the day.
The last line merge all of that in a Matlab standard time serial format. Read the doc on datenum and datestr for more information.

Related

Matlab: Convert file date with milliseconds into Matlab time format

I have some text files with dates of the form
2015-09-08 14:38:03
2015-09-08 14:38:03.1
2015-09-08 14:38:03.2
that I want to convert into Matlab date/time format. As you can see, the text strings have a different time format regarding the milliseconds. In the first case, no milliseconds are given; in the seconds case, milliseconds are given with one digit only. This gives a sampling rate of 20Hz for measuring data.
So far, only
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS');
is working, but of course misses the milliseconds. A conversion like
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
does not work as the milliseconds are either zero (full seconds) or have one digit after the '.' delimiter. A workaround like
x = datestr(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
x = datenum(file.dateColumn, 'yyyy-mm-dd HH:MM:SS.FFF');
i.e. converting the text string into a Matlab string (and giving it additional FFF/FF digits) and then converting that one into a date/time number works - but this is such time-cosuming that I cannot use it for my data. I have millions of time rows in different files.
Do you have any ideas?
Greetings, Arne
Thanks to Nick I found a way to solve it:
dataVelPres = readtable(fileName, ...
'ReadVariableNames', false, ...
'HeaderLines', 4 );
dataVelPres.Properties.VariableNames = {'date' 'velU' 'velV' 'velW' 'pres'};
dateMill = datetime(dataVelPres.date, 'inputformat', 'yyyy-MM-dd HH:mm:ss.S');
dateFull = datetime(dataVelPres.date, 'inputformat', 'yyyy-MM-dd HH:mm:ss');
dateNaT = isnat(dateMill);
dateMill(dateNaT) = dateFull(dateNaT);
dataVelPres.dateTime = dateMill;
dataVelPres.date = datenum(dataVelPres.dateTime); % Convert to number format if needed
This works with two tables - one for millisec. and one without - and puts both together, as both give NaT entries in case the inputformat does not match.
Is there a more elegant way?
You may try something like:
a='2015-09-08 14:38:03';
s=strsplit(a,{'-',':',' '})
x=datenum(cellfun(#str2num,s(1:end)))
I highly recommend using the new datetime object:
strings = {'2015-09-08 14:38:03', '2015-09-08 14:38:03.1', '2015-09-08 14:38:03.2'};
dates = {};
for d = strings
d = d{1};
try
dt = datetime(d, 'inputformat', 'yyyy-MM-dd HH:mm:ss.S');
catch
dt = datetime(d, 'inputformat', 'yyyy-MM-dd HH:mm:ss');
end
dates{end + 1} = dt;
end
>> dates
dates =
[08-Sep-2015 14:38:03] [08-Sep-2015 14:38:03] [08-Sep-2015 14:38:03]
>> dates{end}.Second
ans =
3.2000
It's also easy convert from a datetime object to a datenum:
>> x = [datetime('now'), datetime('yesterday')]
x =
10-Dec-2015 12:53:40 09-Dec-2015 00:00:00
>> datenum(x)
ans =
1.0e+05 *
7.3631 7.3631
>>

Matlab: Add Milliseconds to time vector hh:mm:ss

I have a matrix
M =
hh:mm:ss ms
'12:00:01' 1
'12:00:02' 2
'12:00:03' 3
'12:00:04' 4
'12:00:05' 5
Now I want to add the array of milliseconds ms to my time vector. Like
N =
hh:mm:ss
'12:00:01.001'
'12:00:02.002'
'12:00:03.003'
'12:00:04.004'
'12:00:05.005'
How can I do this? What I tried was:
for k=1:length(M)
t1 = datenum(M{k,1},'HH:MM:SS');
c = num2str(M{k,2});
t2 = datenum(c,'FFF');
time = t1+t2;
N{k,1} = datestr(time,'HH:MM:SS.FFF');
end
But this did not do the job right. What I get is:
N =
hh:mm:ss
'12:00:01.100'
'12:00:02.200'
'12:00:03.300'
...
'12:00:04.100'
'12:00:05.110'
'12:00:05.120'
I think this problem is simple to solve. But at the moment I just do not know how solve it.
It's a string formatting problem.
In your code, instead of
c = num2str(M{k,2});
use
c = sprintf('%03d',M{k,2});
In the above usage, sprintf pads zeros in front of a string if it's less than 3 characters long.

Vectorising Date Array Calculations

I simply want to generate a series of dates 1 year apart from today.
I tried this
CurveLength=30;
t=zeros(CurveLength);
t(1)=datestr(today);
x=2:CurveLength-1;
t=addtodate(t(1),x,'year');
I am getting two errors so far?
??? In an assignment A(I) = B, the number of elements in B and
Which I am guessing is related to the fact that the date is a string, but when I modified the string to be the same length as the date dd-mmm-yyyy i.e. 11 letters I still get the same error.
Lsstly I get the error
??? Error using ==> addtodate at 45
Quantity must be a numeric scalar.
Which seems to suggest that the function can't be vectorised? If this is true is there anyway to tell in advance which functions can be vectorised and which can not?
To add n years to a date x, you do this:
y = addtodate(x, n, 'year');
However, addtodate requires the following:
x must be a scalar number, not a string.
n must be a scalar number, not a vector.
Hence the errors you get.
I suggest you use a loop to do this:
CurveLength = 30;
t = zeros(CurveLength, 1);
t(1) = today; % # Whatever today equals to...
for ii = 2:CurveLength
t(ii) = addtodate(t(1), ii - 1, 'year');
end
Now that you have all your date values, you can convert it to strings with:
datestr(t);
And here's a neat one-liner using arrayfun;
datestr(arrayfun(#(n)addtodate(today, n, 'year'), 0:CurveLength))
If you're sequence has a constant known start, you can use datenum in the following way:
t = datenum( startYear:endYear, 1, 1)
This works fine also with months, days, hours etc. as long as the sequence doesn't run into negative numbers (like 1:-1:-10). Then months and days behave in a non-standard way.
Here a solution without a loop (possibly faster):
CurveLength=30;
t=datevec(repmat(now(),CurveLength,1));
x=[0:CurveLength-1]';
t(:,1)=t(:,1)+x;
t=datestr(t)
datevec splits the date into six columns [year, month, day, hour, min, sec]. So if you want to change e.g. the year you can just add or subtract from it.
If you want to change the month just add to t(:,2). You can even add numbers > 12 to the month and it will increase the year and month correctly if you transfer it back to a datenum or datestr.

Difference in minutes between 2 dates and times?

I need to compute time difference in minutes with four input parameters, DATE_FROM, DATE_TO, TIME_FROM, TIME_TO. And one output parameter DIFF_TIME. I have created a function module, I need to write a formula which computes the time diff in minutes.
Any help would be great!
Thanks,
Sai.
Use CL_ABAP_TSTMP=>TD_SUBTRACT to get the number of seconds between two date/time pairs.
(then, to get the number of minutes, divide the number of seconds by 60).
Example:
DATA(today_date) = CONV d( '20190704' ).
DATA(today_time) = CONV t( '000010' ).
DATA(yesterday_date) = CONV d( '20190703' ).
DATA(yesterday_time) = CONV t( '235950' ).
cl_abap_tstmp=>td_subtract(
EXPORTING
date1 = today_date
time1 = today_time
date2 = yesterday_date
time2 = yesterday_time
IMPORTING
res_secs = DATA(diff) ).
ASSERT diff = 20. " verify expectation or short dump
If the values are guaranteed to be in the same time zone, it's easy enough that you don't need any special function module or utility method. Read this, then get the difference of the dates and multiply that by 24 * 60 and get the difference of the times (which is in seconds) and divide that by 60. Sum it up and there you are.

find mean or median date of event

I have a dataset for which I have extracted the date at which an event occurred. The date is in the format of MMDDYY although MatLab does not show leading zeros so often it's MDDYY.
Is there a method to find the mean or median (I could use either) date? median works fine when there is an odd number of days but for even numbers I believe it is averaging the two middle ones which doesn't produce sensible values. I've been trying to convert the dates to a MatLab format with regexp and put it back together but I haven't gotten it to work. Thanks
dates=[32381 41081 40581 32381 32981 41081 40981 40581];
You can use datenum to convert dates to a serial date number (1 at 01/01/0000, 2 at 02/01/0000, 367 at 01/01/0001, etc.):
strDate='27112011';
numDate = datenum(strDate,'ddmmyyyy')
Any arithmetic operation can then be performed on these date numbers, like taking a mean or median:
mean(numDates)
median(numDates)
The only problem here, is that you don't have your dates in a string type, but as numbers. Luckily datenum also accepts numeric input, but you'll have to give the day, month and year separated in a vector:
numDate = datenum([year month day])
or as rows in a matrix if you have multiple timestamps.
So for your specified example data:
dates=[32381 41081 40581 32381 32981 41081 40981 40581];
years = mod(dates,100);
dates = (dates-years)./100;
days = mod(dates,100);
months = (dates-days)./100;
years = years + 1900; % set the years to the 20th century
numDates = datenum([years(:) months(:) days(:)]);
fprintf('The mean date is %s\n', datestr(mean(numDates)));
fprintf('The median date is %s\n', datestr(median(numDates)));
In this example I converted the resulting mean and median back to a readable date format using datestr, which takes the serial date number as input.
Try this:
dates=[32381 41081 40581 32381 32981 41081 40981 40581];
d=zeros(1,length(dates));
for i=1:length(dates)
d(i)=datenum(num2str(dates(i)),'ddmmyy');
end
m=mean(d);
m_str=datestr(m,'dd.mm.yy')
I hope this info to be useful, regards
Store the dates as YYMMDD, rather than as MMDDYY. This has the useful side effect that the numeric order of the dates is also the chronological order.
Here is the pseudo-code for a function that you could write.
foreach date:
year = date % 100
date = (date - year) / 100
day = date % 100
date = (date - day) / 100
month = date
newdate = year * 100 * 100 + month * 100 + day
end for
Once you have the dates in YYMMDD format, then find the median (numerically), and this is also the median chronologically.
You see above how to present dates as numbers.
I will add no your issue of finding median of the list. The default matlab median function will average the two middle values when there are an even number of values.
But you can do it yourself! Try this:
dates; % is your array of dates in numeric form
sdates = sort(dates);
mediandate = sdates(round((length(sdates)+1)/2));