Matlab: Selecting rows related of specfic time - matlab

I have data from a sensor, data were recording for five days non stop. How to select specific rows, related with the certain time, e.g. 11-09-2013 11:20:00 - 11:21:49 PM ? Then, how to divide the obtained data into equal segments 2.56 seconds each?
My data looks like this:
0.135478690266609
0.0537606552243232
-0.0262537784874439
0.157014295458793
0.149360358715057
0.104898564517498
0.0393946692347526
...
and I can read time by using datestr(data(1), 'dd-mm-yyyy HH:MM:SS AM')

If I understand correctly, data is a vector of timestamps in days.
To find the first row that comes after 11-09-2013 11:20:00 PM, you can do:
target = datenum('11-09-2013 11:20:00 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
row = find(data >= target, 1, 'first');
To find all rows between 11-09-2013 11:20:00 PM and 11-09-2013 11:21:49 PM:
start = datenum('11-09-2013 11:20:00 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
stop = datenum('11-09-2013 11:21:49 PM', 'dd-mm-yyyy HH:MM:SS AM') / 86400;
rows = find(data >= start & data <= stop);
To get a segment of 2.56 seconds after the date start, set stop to start + 2.56 / 86400 and use the same trick as above.

Related

How to calculate Duration of Time between two date time through VBscript

How to calculate Duration of Time between two date time through VBscript
Date1 = 2021-01-22 11:43:38.000
Date2 = 2021-01-22 14:32:38.000
result should be HH:MM:SS
TimeSerial and FormatDateTime return a date or time that will take the regional settings of the computer into account. On my European computer no AM extension is shown because we use the 24h time format.
An additional problem with TimeSerial is that it will overflow once there are more than 32767 seconds.
A different approach could be to calculate the values for hours, minutes and seconds separately. A possible solution could be:
secValue = DateDiff("s",Date1,Date2)
hours = secValue \ 3600
hh = hours
if hours < 10 then
hh = Right("0" & hours, 2)
end if
mm = Right("0" & (secValue - hours * 3600) \ 60, 2)
ss = Right("0" & secValue mod 60, 2)
diff = hh & ":" & mm & ":" & ss
wscript.echo diff
Finally i answered to my Question Feeling great
Date1 = alA_Filling(0)
Date2 = alA_Filling(1)
secValue = DateDiff("s",Date1,Date2)
ts = TimeSerial(0, 0, secValue)
Duration = FormatDateTime(ts, vbLongTime)
But i got output like 2:49:00 AM why added AM to this may be this vbLongTime
how can i remove that AM

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

Find Date Range Rows and Extract

This should be straight-forward in MATLAB I just don't know how and am stuck. I have data that looks like this below that is in the form: mm/dd/yyyy hh:mm windspeed - this is hourly data spanning years from 1991 up to the present (2013) of cell arrays and numeric array:
8/22/1993 23:00 2.381453514
8/23/1993 0:00 3.39369154
8/23/1993 1:00 5.398661613
8/23/1993 2:00 7.231492996
8/23/1993 3:00 9.187699318
8/23/1993 4:00 9.802619934
8/23/1993 5:00 8.85418129
8/23/1993 6:00 9.889941216
8/23/1993 7:00 10.4628706
8/23/1993 8:00 10.8967123
8/23/1993 9:00 10.12729263
8/23/1993 10:00 9.106620789
8/23/1993 11:00 7.600066185
8/23/1993 12:00 6.597990036
8/23/1993 13:00 6.764455795
8/23/1993 14:00 7.360760689
8/23/1993 15:00 5.828835011
I am trying to extract the third column only (windspeed). I need to be able to change the date range to extract a month at a time for example ALL the rows containing the month "08" for August of the year 1993 and then for future use all the rows containing "09" for September and the year 2013. I'm not sure if it's better to use datenum or the find function and then how to code this for either case.
I am using xlsread to read the .csv file with a portion of the data shown above.
I don't know how you import your data now, and what your variables in the workspace are, but here's a way to do it:
Find the xxx.csv-file in the Current Folder window in Matlab. Double click it to import it. In the window that appears, you can select Delimiter: Space. Play around with the settings, to find the "best fit"
Now, you have your variable in the Matlab workspace.
hhv = datevec(hh);
% You are interested in column 4, HOUR:
% hhv is a matrix with [2013, 1, 1, HOUR, 0, 0]
Now, if you want all windspeeds between 0:00 and 06:00 08/23/1993:
winds = ws(dt == datenum(1993,8,23) & hhv(:,4) >= 0 & hhv(:,4) < 6)
winds =
3.3937
5.3987
7.2315
9.1877
9.8026
8.8542
A generic way to read the whole file could be:
fid = open('data.csv');
% %d = integer, %f = float
data = textscan(fid, '%d/%d/%d %d:%d %f');
fclose(fid);
% all months in data
disp(data{1});
% all wind speeds in data
disp(data{end});
You can restrict the returned data by modifying the matching pattern in textscan:
% only August
mask = data{1} == 8;
speeds = data{end}(mask);
% June of 1997
mask = (data{1} == 6) & (data{3} == 1997);
speeds = data{end}(mask);
See more here: http://www.mathworks.com/help/matlab/ref/textscan.html#btg0kes

Converting Epoch to Date in Matlab

I have an array of Epoch milliseconds (array of numbers) in Matlab. I would like to convert these into UTC date-time format, such as DD-MM-YYYY HH:MM.
Is there a pre-defined Matlab way to do this or will I have to write my own function?
Suppose, you start with a vector time_unix, then:
>> time_unix = 1339116554872; % example time
>> time_reference = datenum('1970', 'yyyy');
>> time_matlab = time_reference + time_unix / 8.64e7;
>> time_matlab_string = datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF')
time_matlab_string =
20120608 00:49:14.872
Notes:
1) See the definition of matlab's time.
2) 8.64e7 is number of milliseconds in a day.
3) Matlab does not apply any time-zone shifts, so the result is the same UTC time.
4) Example for backward transformation:
>> matlab_time = now;
>> unix_time = round(8.64e7 * (matlab_time - datenum('1970', 'yyyy')))
unix_time =
1339118367664
To summarize, here are two functions:
function tm = unix2matlab(tu)
tm = datenum('1970', 'yyyy') + tu / 864e5;
end
function tu = matlab2unix(tm)
tu = round(864e5 * (tm - datenum('1970', 'yyyy')));
end
The matlab time here is numeric. You can always convert it to string using datestr()
Update for nanoseconds
time_unix_nanos = 1339116554872666666;
millis = round(time_unix_nanos / 1e6);
nanos = time_unix_nanos - 1e6 * millis;
time_matlab = unix2matlab(millis);
s = [datestr(time_matlab, 'yyyymmdd HH:MM:SS.FFF'), num2str(nanos)];
s =
20120608 00:49:14.872666666
I tried the above code, but the results were wrong. I realised the main error is related to the awkward definition of the Unix time (epoch time). Unix time (epoch time) is defined as the number of seconds after 1-1-1970, 00h:00, not the number of **milli**seconds (http://en.wikipedia.org/wiki/Unix_time). With this definition, the Unix time should therefore be divided by 8.64e5 instead of 8.64e7.
In addition, datenum('1970', 'yyyy') does not seem to result in the desired reference time of 1-1-1970, 00h:00.
Here's my improved code:
tMatlab = datenum (1970,1,1,0,0) + tUnix / 86400;
Serg's answer is what I normally use, when I'm working in MATLAB. Today I found myself wanting to do the conversion to date in MATLAB as the title says - without the datestring conversion  specified in the question body - and output the date number from the shell.
Here is what I settled on for the rounded date number:
TODAY_MATLAB="$[719529 + $[`date +%s` / 24/60/60]]"
This is really just the bash equivalent of what you would expect: 719529 is the datenum of the epoch (1970-01-01 or datenum(1970,1,1) in MATLAB). I'm also fumbling through ksh lately and it seems this can be done there with:
TODAY_EPOCH=`date +%s`
TODAY_MATLAB=`expr $TODAY_EPOCH / 24 / 60 / 60 + 719529`
As a side exercise, I added the decimal portion back onto the date in bash - I didn't bother in ksh, but it's only arithmetic and goes similarly:
N_DIGITS=7
FORMAT=$(printf "%%d.%%0%dd" $N_DIGITS)
NOW_EP_SEC=`date +%s`
SEC_PER_DAY=$(( 24*60*60 ))
NOW_EP_DAY=$(( $NOW_EP /$SEC_PER_DAY ))
SEC_TODAY=$(( $NOW_EP_SEC - $NOW_EP_DAY*$SEC_PER_DAY ))
TODAY_MATLAB="$(( NOW_EP_DAY+719529 ))"
FRACTION_MATLAB="$( printf '%07d' $(( ($SEC_TODAY*10**$N_DIGITS)/SEC_PER_DAY )) )"
MATLAB_DATENUM=$( printf $FORMAT $TODAY_MATLAB $FRACTION_MATLAB )
echo $MATLAB_DATENUM

How do I convert microseconds into a timestamp?

I took this piece from an unencrypted .DAT file:
Code:
00 e1 27 17 6f e6 69 c0
Which translates to 63,374,851,375,000,000 in decimal. The units for the number are microseconds.
And this huge number cannot bypass the 1st January 1970 00:00:00 format; such a format that most converters use today.
So, yes. Is there such a converter that uses the 1st January of the year 1 format? Or how shall I make one?
And by the way, a timestamp is both date and time.
Thanks in advance!
You do not say what language are you using, if it is a .NET language, you can use: http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx for that constructor the input is in nanoseconds (are you sure that your number is in milliseconds and not in nanoseconds?).
If you are sure it is in milliseconds, the conversion to nanoseconds should be easy: 1 millisecond = 1 000 000 nanoseconds.
But I have the feeling that those are nanoseconds and not milliseconds...
Now that you have told us that it is in microseconds:
C# Example from decimal to yyyy dd MM hh:mm:ss
long microseconds = 63370738175000000;
long ticks = microseconds * 10;
DateTime timestamp = new DateTime(ticks);
Console.WriteLine(timestamp.ToString("yyyy dd MM hh:mm:ss"));
It prints:
2009 20 02 02:49:35
The other way around from yyyy dd MM hh:mm:ss to decimal
String dateString = "2009 20 02 02:49:35";
DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd MM hh:mm:ss",CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
Console.WriteLine(microseconds);
It prints:
63370694975000000
And if you want it in hexadecimal just write:
Console.WriteLine(microseconds.ToString("X"));
Then it will print:
E1234FB3278DC0
If you want the answer in another programming language, please add that to you question.
In JAVA in order to convert microseconds into java.sql.Timestamp:
public static Timestamp getTimestampFromMicros(long pMicros) {
long millis = TimeUnit.MICROSECONDS.toMillis(pMicros);
long shaaritInMicros = pMicros - TimeUnit.MILLISECONDS.toMicros(millis);
Timestamp ts = new Timestamp(millis);
long nanos = ts.getNanos() + TimeUnit.MICROSECONDS.toNanos(shaaritInMicros);
ts.setNanos((int)nanos);
return ts;
}
Use below Java code to covert microseconds to date and time,
long msec = microseconds * 1/1000;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.format(msec);
Which will returns,
2016-01-27 03:41:12