Calculate Difference of Timestamps - matlab

I want to calculate time difference with the previous line, however, the result is not as expected. For example, the result in deltatime(2,5) (see first screenshot) is supposed to be 3 instead of 13 (17:47-17:44).
My code:
% Calculate connection time
sizedate = length(date);
for i=2:sizedate
X=char(date(i));
X=[X(1) X(2)];
if(X=='18')
timenum_on=char(time(i));
timenum_b4=char(time(i-1));
if(timenum_on(5)<timenum_b4(5) || timenum_on(1) >=timenum_b4(1)
timenum_on(5)=timenum_on(5)+10;
timenum_on(4)=timenum_on(4)-1;
deltatime(i,")=timenum_on-timenum_b4;
else
timenum_b4(5)=10-timenum_b4(5);
deltatime(i,:)=timenum_on-timenum_b4;
end

Why not simply subtract/diff the datetime objects directly to get duration objects? Here's a demonstration:
function out = q48740661
dt = datetime(2017, 02, 12, 17, 43 + (0:4).^2, 12).';
out = flipud(diff(flipud(dt)));
ans =
4×1 duration array
-0:01:00
-0:03:00
-0:05:00
-0:07:00

Try creating this function:
deltatime=func_time(time);
Contents of this function:
function time_diff= func_time(time_arr)
n=size(time_arr(:,1));
y=n(1);
time_diff=zeros(n,n);
for i=1:y
for j=1:y
time_1=time_arr(i,:); %char array of first time
time_2=time_arr(j,:); %char array of second time
time_cell=time_1;
ctr=1;
for k=1:size(time_1(:)+1)
if(time_1(k)==':')
av=str2num(time_1(ctr:(k-1)));
bv=str2num(time_2(ctr:(k-1)));
time_cell(ctr:(k-1))=int2str(av-bv)
ctr=k+1
end
if(k==5)
av=str2num(time_1(ctr:(k)));
bv=str2num(time_2(ctr:(k)));
time_cell(ctr:k)=int2str(av-bv)
end
end
time_diff(i,j)=time_cell();
end
end
Here I am assuming that deltatime(i,j) is time(i)-time(j)

Related

REXX codin with CSV input

Im new to coding with REXX and I need to make a program to find the Max, min and avg temperature for every month.
The input is Comma separated like below:
DAY/MONTH,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
1st,25.9,25.4,31.3,22.4,8.1,7.3,12.9,13.1,11.3,15.2,19.2,21
2nd,27.2,22.3,18,14.6,10.2,10.9,10.9,13.5,15.9,24.9,26.2,17.2
3rd,34.9,16.6,19.1,20.8,10.6,10.7,7,11.1,14.5,25.1,28.9,22.9
4th,24.4,19.8,21.7,12.6,11.5,13,10.5,7.3,13.1,22.5,16.8,21.7
5th,14.1,21.8,18.9,14.4,15.4,11.7,10.5,8.4,14,11.4,13.8,23.4
... etc
I need to create REXX code to find the the Max, Min and Mean temperature for each month and present it like below
User,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Max,34.9,16.6,19.1,20.8,10.6,10.7,7,11.1,14.5,25.1,28.9,22.9
Min,24.4,19.8,21.7,12.6,11.5,13,10.5,7.3,13.1,22.5,16.8,21.7
Mean,14.1,21.8,18.9,14.4,15.4,11.7,10.5,8.4,14,11.4,13.8,23.4
Any help in creating the REXX code, or any literature/direction on it would be greatly appreciated.
So far my code is
/*REXX*/
/* TRACE ?I */
ADDRESS SYSCALL "READFILE /u/inputdata RECS."
IF RC <> 0 THEN DO
SAY "ERROR READING FILE"
EXIT
END
FS=","; MAXTEMP=""; MINTEMP=""; AVGTEMP=""
DO I = 2 TO RECS.0
PARSE VAR RECS.I DAY","JAN","FEB","MAR","APR","JUN","JUL","AUG","SEP","OCT","NOV,"DEC
DO J = 2 TO RECS.I
MAXTEMP = MAX(RECS.I) /*Needs to add another VAR into Maxtemp*/
MINTEMP = MIN(RECS.I) /*same but Min */
AVGTEMP = SUM(RECS.I)/COUNT(RECS.I) /*Total/The amount of days*/
END
END
SAY user, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
SAY MAX, MAXTEMP /*MAX for each month fill out*/
SAY MIN, MINTEMP /*Min */
SAY MEAN, AVGTEMP /*Avg */
END
Im trying to make a variable for MaxTemp, MinTemp and MeanTemp adding the months on as the loop goes.
Here is a possible implementation. It runs over each record as you did; this is the outer loop (loop variable ii). Then the current temperature for each month is
compared to the current maximum for that month, and stored if greater
compared to the current minimum for that month, and stored if lower
added to the average sum-up field for the month
This is the inner loop (lop variable jj). Note that the code inside this inner loop takes care not to process values for days beyond the maximum number of days per month.
Since the year for the data doesn't seem to be known, the correct number of days for February cannot be calculated. You would need to add that, if the year is known.
/*REXX*/
/* TRACE ?I */
address syscall "readfile /u/inputdata Recs."
if RC <> 0
then do
say "ERROR READING FILE"
exit 16
end
ifs = "," /* Field separator for parsing input records */
ofs = "," /* field separator for writing output records */
/* Initialize arrays to aggregate temperature data by month */
MaxTemp. = 0
MinTemp. = 99999
AvgTemp. = 0
/* Initialize array of number of days per month */
/* We do not know the year, so we cannot calculate whether February has 28 or 29 days */
DaysPerMonth.1 = 31
DaysPerMonth.2 = 28 /* Need to be adjusted for leap years */
DaysPerMonth.3 = 31
DaysPerMonth.4 = 30
DaysPerMonth.5 = 31
DaysPerMonth.6 = 30
DaysPerMonth.7 = 31
DaysPerMonth.8 = 31
DaysPerMonth.9 = 30
DaysPerMonth.10 = 31
DaysPerMonth.11 = 30
DaysPerMonth.12 = 31
/* Split (parse) each input record and fill the DayTemp array with daily temperatures by month */
do ii = 2 to Recs.0
parse var Recs.ii DayNum (ifs) DayTemp.1 (ifs) DayTemp.2 (ifs) DayTemp.3 (ifs) DayTemp.4 ,
(ifs) DayTemp.5 (ifs) DayTemp.6 (ifs) DayTemp.7 (ifs) DayTemp.8 ,
(ifs) DayTemp.9 (ifs) DayTemp.10 (ifs) DayTemp.11 (ifs) DayTemp.12 .
/* For each month, adjust min and max values, and sum up for building average later on */
do jj = 1 to 12
/* Don't process values for day numbers greater that number of days in month */
if ( ii - 1 ) <= DaysPerMonth.jj
then do
if MaxTemp.jj < DayTemp.jj
then MaxTemp.jj = DayTemp.jj
if MinTemp.jj > DayTemp.jj
then MinTemp.jj = DayTemp.jj
AvgTemp.jj = AvgTemp.jj + DayTemp.jj
end /* if ( ii - 1 ) ... */
end /* do jj = 1 to 12 */
end /* do ii = 1 to 12 */
Heading = "User,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
Maxima = "Max"
Minima = "Min"
Means = "Mean"
/* Build the min, max and average records by appending value by value */
do ii = 1 to 12
Maxima = Maxima || ofs || format( MaxTemp.ii, 2, 1 )
Minima = Minima || ofs || format( MinTemp.ii, 2, 1 )
Means = Means || ofs || format( ( AvgTemp.ii / DaysPerMonth.ii ), 2, 1 )
end
/* Write heading, min, max, and average records */
say Heading
say Maxima
say Minima
say Means
Tip: Don't use single letter variable names, since you cannot search, or search-and-replace such names. I always use double letter variable names for loop variables, or temporary variables, such as ii, jj, kk, etc. These (most probably) never appear in the code in any other context, so searching for, and replacing can be easily done.

Matlab Code for weekdays and weekends [duplicate]

This question already has an answer here:
Weekend extraction in Matlab
(1 answer)
Closed 6 years ago.
I were able to successfully made a schedule in which the output is 1 if time is between 7 AM-5PM and otherwise 0, time is based on my computer. However the day Monday-Sunday is based on my computer as well.. I cant find the solution to put an output 1 on Monday-Saturday and output 0 on Sunday. The code I have is below
function y = IsBetween5AMand7PM
coder.extrinsic('clock');
time = zeros(1,6);
time = clock;
current = 3600*time(4) + 60*time(5) + time(6); %seconds passed from the beginning of day until now
morning = 3600*7; %seconds passed from the beginning of day until 7AM
evening = 3600*17; %seconds passed from the beginning of day until 5PM
y = current > morning && current < evening;
end
Now the time here is correct already what I need is for the day (Monday-Sunday) to have my needed output. Also this matlab code is inside a matlab function on Simulink block.
If you use weekday like this, you can generate a 0/1 value as you specified for today's date:
if (weekday(now) > 1)
day_of_week_flag = 1;
else
day_of_week_flag = 0;
or if you like, this one-liner does the same thing, but may not be as easy to read if you're not familiar with the syntax:
day_of_week_flag = ( weekday(now) > 1);
You can also use date-strings like this to convert other dates:
day_of_week_flag = ( weekday('01-Mar-2016') > 1 )
Finally, if you have a numeric array of date/time values, like [2016 3 3 12 0 0], you first need to convert to a serial date using datenum, then use weekday:
time = clock;
day_of_week_flag = ( weekday(datenum(time)) > 1);
An alternate way to check without using weekday is the following:
time = clock;
day_of_week = datestr(time, 8);
if (day_of_week == 'Sun')
day_of_week_flag = 0;
else
day_of_week_flag = 1;

macro to extract dates from a weeks date range string and add 7 days to print next date range

I am writing a macro that processes an excel with lots of data. One of the rows contains a date range like wkstartdate - wkenddate and I would like to use dateadd function to print next date range every week (like '27-01-14 - 02-02-14' in below case) but unable to do so.
'06-01-14 - 12-01-14'
'13-01-14 - 19-01-14'
'20-01-14 - 26-01-14'
I used below excerpt which fails:
Range("E" & Lastrow).Select
prwk = Split(ActiveCell.Value, "-")
'curr_wkstart = DateAdd("d", 7, prwk(1)) 'error as maybe prwk(1) isnt correct format
'curr_wkend = DateAdd("d", 7, prwk(2)) 'error
Range("E" & Lastrow + 1).Value = curr_wkstart & curr_wkend 'no result
For testing purpose I print, prwk(1) which is 20/01/14 in the above case, in a diff cell and add 7 days, which gives me 1/21/2020 instead of '27/01/14'. I also tried using Cdate function, but still error
Can you please advise??
I think what you want to use here are the Format and DateSerial functions. Here's how I came at it:
Function GetNextWeek(TheStartWeek)
a = Split(TheStartWeek, " - ")
b = Split(a(1), "-")
c = DateSerial(b(2), b(1), b(0)) + 1
d = c + 6
GetNextWeek = Format(c, "dd-mm-yy") & " - " & Format(d, "dd-mm-yy")
End Function
Sub Test()
Debug.Print GetNextWeek("13-01-14 - 19-01-14") 'Givs you "20-01-14 - 26-01-14"
End Sub
Hope this helps.

Matlab: Converting Timestamps to Readable Format given the Reference Date-Time

I have a text file that contains timestamps out of a camera that captures 50 frames per second .. The data are as follows:
1 20931160389
2 20931180407
3 20931200603
4 20931220273
5 20931240360
.
.
50 20932139319
... and so on.
It gives also the starting time of capturing like
Date: **02.03.2012 17:57:01**
The timestamps are in microseconds not in milliseconds, and MATLAB can support only till milliseconds but its OK for me.
Now I need to know the human format of these timestamps for each row..like
1 20931160389 02.03.2012 17:57:01.045 % just an example
2 20931180407 02.03.2012 17:57:01.066
3 20931200603 02.03.2012 17:57:01.083
4 20931220273 02.03.2012 17:57:01.105
5 20931240360 02.03.2012 17:57:01.124
and so on
I tried this:
%Refernce Data
clc; format longg
refTime = [2012,03,02,17,57,01];
refNum = datenum(refTime);
refStr = datestr(refNum,'yyyy-mm-dd HH:MM:SS.FFF');
% Processing data
dn = 24*60*60*1000*1000; % Microseconds! I have changed this equation to many options but nothing was helpful
for i = 1 : size(Data,1)
gzTm = double(Data{i,2}); %timestamps are uint64
gzTm2 = gzTm / dn;
gzTm2 = refNum + gzTm2;
gzNum = datenum(gzTm2);
gzStr = datestr(gzNum,'yyyy-mm-dd HH:MM:SS.FFF'); % I can't use 'SS.FFFFFF'
fprintf('i = %d\t Timestamp = %f\t TimeStr = %s\n', i, gzTm, gzStr);
end;
But I got always strange outputs like
i = 1 Timestamp = 20931160389.000000 TimeStr = **2012-03-08 13:29:28.849**
i = 2 Timestamp = 20931180407.000000 TimeStr = **2012-03-08 13:29:29.330**
i = 3 Timestamp = 20931200603.000000 TimeStr = **2012-03-08 13:29:29.815**
The output time is about some hours late/earlier than the Referenced Time. The day is different.
The time gap between each entry in the array should be nearly 20 seconds..since I have 50 frames per second(1000 millisecond / 50 = 20) ..and the year,month, day,hour,minute and seconds should also indicate the initial time given as reference time because it is about some seconds earlier.
I expect something like:
% just an example
1 20931160389 02.03.2012 **17:57:01.045**
2 20931180407 02.03.2012 **17:57:01.066**
Could one help me please..! Where is my mistake?
It looks like you can work out the number of microseconds between a record and the first record:
usecs = double(Data{i,2}) - double(Data{1,2});
convert that into seconds:
secsDiff = usecs / 1e6;
then add that to the initial datetime you'd calculated:
matDateTime = refNum + secsDiff / (24*60*60);

Convert date to closest end-of-month date MATLAB

I need to convert a datenumber to its closest end-of-month date. I found an online link but it is very inefficient for a large matrix (at http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times). Does Matlab (Financial Toolbox) has an inbuilt function for this? I couldn't find it.
date_in = 734421 ;
somefunction(date_in) --> Sept 2010
Thanks!
Basically, it sounds like you are asking for whether a given date is closer to the preceding or following month. You can greatly simplify the logic involved if you use the functions EOMDAY to find the date for the end of the month and ADDTODATE to shift the current month up or down by one. Here's an example function that takes a date number as input:
function closestString = closest_month(dateNumber)
dateVector = datevec(dateNumber);
daysInMonth = eomday(dateVector(1),dateVector(2));
if dateVector(3) > daysInMonth/2
dateNumber = addtodate(dateNumber,1,'month');
else
dateNumber = addtodate(dateNumber,-1,'month');
end
closestString = datestr(dateNumber,'mmm yyyy');
end
I had some errors in my previous version. Here's the logic incorporated into a function. It also checks for the month and updates accordingly.
function out = roundMonth(dateNumber)
dateVector = datevec(dateNumber);
day = dateVector(3);
month = dateVector(2);
year = dateVector(1);
month = month + sign(day - 15 + double(~(month-2)))...
+ double(~(day-15 + double(~(month-2))));
dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
dateVector(2) = mod(month,12) + 12*double(~mod(month,12));
out = datestr(dateVector,'mmm yyyy');
EXAMPLES:
1.
roundMonth(datenum('10-Oct-2010'))
ans =
Sep 2010
2.
roundMonth(datenum('20-Oct-2010'))
ans =
Nov 2010
3.
roundMonth(datenum('20-Dec-2010'))
ans =
Jan 2011
4.
roundMonth(datenum('10-Jan-2010'))
ans =
Dec 2009