How to do calculation on time values imported from Excel? - matlab

I have the following values in Excel:
Bed time 19:34:00
Get up time 07:04:00
Time in bed 11:30:00
Sleep start 19:42:00
Sleep end 07:00:00
I want to import them into MATLAB and do some calculation on these time values such as subtraction. The time values look like this after importing:
0.8153
0.2944
0.4792
0.8208
0.2917
and obviously doing calculation on them would be nonsense. Would any body help me with this issue? I have stuck with it for few days, and no progress yet.
Thanks in advance,

As assylias pointed out, these are fractions of days. You can use the datestr function to convert it to human-readable strings with formatting option conveniently.
e.g.:
datestr(0.2917, 'HH:MM:SS')
ans =
07:00:02
Calculations such as subtractions can be done on the raw values before
conversion.
E.g: get duration of sleep.
start = 0.8208
stop = 0.2917
datestr(stop-start, 'HH:MM')
ans =
11:18
Even works for intervals that span over midnight.

Related

Is there an easy way to read integer time data from a netcdf file using the time-attribute?

I am trying to read time-coordinate data from a netCDF file using matlab. I have a netCDF file (which I created) that has a time variable in the format of a double corresponding to the number of hours from a specific time (see below).
Variable attributes:
double time(Time) ;
time:standard_name = "Time" ;
time:units = "hours since 2002-01-01 0:0:0" ;
time:calendar = "proleptic_gregorian" ;
When I read the time variable using ncread) into matlab, it just prints out an integer e.g.,1. However, if I use "ncdump" to explore the file, I see the time variable in it's coordinate time e.g., 2002-01-01 01.
Specifically: "ncdump -t -v time ncfile.nc"
I'm relatively new to matlab, and I was wondering if anyone knew if there was a similar, or an equally simple, way to read this time variable as its coordinate time into matlab, either as a string, or numerical date. Specifically, I would like to avoid having to parse the attribute string and code up a bunch of pointers and conditions to convert the integer data to an actual date.
Alternatively, should I just create a new time variable in these files that is just an array of dates as strings?
Any information is very much appreciated!
Thanks!
NetCDF stores time as an offset from an epoch. From your variable attribute, your epoch is 2002-01-01 0:0:0, and the time is hours since then. Matlab has a similar methodology called date numbers, although it is based off of days since an epoch (which they call pivot years). There are two functions that you should look into: datenum and datestr. The first converts a string into a date number and the other converts a date number into a date string.
You can convert your time variable into a compatible Matlab date number by dividing by 24 and then use the datestr function to format it however you like. Here is a simple example:
>> time = [1;2;3;4];
>> datestr(time./24+datenum('2002-01-01 0:0:0'))
ans =
01-Jan-2002 01:00:00
01-Jan-2002 02:00:00
01-Jan-2002 03:00:00
01-Jan-2002 04:00:00
Look at the Matlab help files associated with the two functions and you can format the date output however you like.

Iterate for loop by hour in MATLAB

I am writing a for loop to average 10 years of hourly measurements made on the hour. The dates of the measurements are recorded as MATLAB datenums.
I am trying to iterate through using 0.0417 as it is the datenum for 1AM 00/00/00 but it is adding in a couple of seconds of error each time I iterate.
Can anyone recommend a better way for me to iterate by hour?
date = a(:,1);
load = a(:,7);
%loop for each hour of the year
for i=0:0.0417:366
%set condition
%condition removes year from current date
c = date(:)-datenum(year(date(:)),0,0)==i;
%evaluate condition on load vector and find mean
X(i,2)=mean(load(c==1));
end
An hour has a duration of 1/24 day, not 0.0417. Use 1/24 and the precision is sufficient high for a year.
For an even higher precision, use something like datenum(y,1,1,1:24*365,0,0) to generate all timestamps.
To avoid error drift entirely, specify the index using integers, and divide the result down inside the loop:
for hour_index=1:365*24
hour_datenum = (hour_index - 1) / 24;
end

Matlab convert date string to timestamps using datenum problems

I am new to Matlab. I am trying to use datenum function to parse date string and convert to timestamps(like in Java, getTime()).Then, I want to find out the difference between two dates in seconds.
datenum('2013-02-21T00:39:19Z','yyyy-mm-ddTHH:MM:ss')-datenum('2013-02-21T00:34:19Z','yyyy-mm-ddTHH:MM:ss')
If I run the function above, I get 0.0035 which I have no idea what kind of value it is.
Can someone help please?
Thanks!
Matlab help says:
A serial date number represents the whole and fractional number of
days from a fixed, preset date (January 0, 0000).
I reckon your answer is maybe 0.0035 days so to get seconds I guess its
ans*24*60*60
Your result is in datenum format as Dan says. But if you want to find elapsed time in seconds, there is a function that does exactly what you want.
You can use etime to find elapsed time between two date vectors.
d1 = datevec('2013-02-21T00:39:19Z','yyyy-mm-ddTHH:MM:ss');
d2 = datevec('2013-02-21T00:34:19Z','yyyy-mm-ddTHH:MM:ss');
elapsedTime = etime(d1,d2) % Elapsed time in seconds
elapsedTime =
300

datetime format in matlab

I have some data that was given to me in excel and the time format is rather confusing. The fist column of the is the DateTime but with incorrect HH:MM and the second column is the correct hour of the day HH:MM:
time = {'01/01/2000 00:00',num2str(2300);
'01/01/2000 00:00',num2str(2400);
'01/01/2000 00:00',num2str(10);
'01/01/2000 00:00',num2str(100)};
However, when the time exceeds midnight, instead of being 00:10 the time is 10, and 01:00 is 100. How could I alter these to the correct format? i.e. from the example above I would like the outcome to be:
time = {'01/01/2000 23:00';
'01/01/2000 24:00';
'01/01/2000 00:10';
'01/01/2000 01:00'};
How could I achieve this?
Using sprintf in MATLAB, you can use the field width specifier:
where
Field width:
Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%12d', intmax) is equivalent to ('%*d', 12, intmax).
Thus your times should end up all looking like "XX:XX", with the leading zero added by the sprintf if it is missing and the colon added in.
Thanks to #Junuxx for the exact command: sprintf('%02i:%02i', hours, minutes)
To separate hours and minutes, you would obviously do time % 100 to get the minutes and integer divide by 100 to get the hours.
From there, you simply strcat or concatenate ["a" "b"] your two columns to get your desired result.

Generate a periodic value based on dates

I was hoping someone that is good with math and loops could help me out. I'm writing a program in Objective C where I need to come up with a way to do a cycle. If you don't know Objective C I would appreciate any help in pseudo code just to help me figure this out.
What I need is a scale that is based on two dates. I know this will be some sort of loop but not sure how to figure it out.
For instance, lets say that the first date is 5/25/1976 and the second date is 9/25/2009. Every 25 days there will be a "peak" so it's value will be 100. If I divide 23 in half I get 12 (rounded) so it would be the opposite or "valley" so it's numerical value would be 0. In other words on the 23rd day it would be at 100 but then on the 24th day it would start going back down and then bottom out 12 days later and then start the cycle back up and top out again at 23 days.
What I need to be able to do is find the numerical value for any given date in between any two given dates.
Thanks for any help you can offer!
value = 100*cos(2*pi*(numDays/25))
Or something like that.
Calculate the difference in days (optionally in fractional days too) between the starting point and the day you want the value for.
Divide by the cycle period (could be 23 or 25 according to the question).
Take the fractional part.
Apply the correct periodic function - for example, either sin() or cos(), appropriately scaled for the trigonometric functions (multiply the fraction by 2π).
You could simulate the shape by values out of a table describing the values indexed on days into the period (so you would use waveform[Δt mod period] to determine the value).
The NSDate class has a method timeIntervalSinceDate that will give you then number of seconds between two dates. You could calculate the number of days between two dates like this:
- (double) daysBetweenStart:(NSDate*)start end:(NSDate*)end
{
return [start timeIntervalSinceDate:end] / 86400.0; // seconds in a day
}
You could use this to compute a step function based on that:
- (double) someDescriptiveFunctionName:(NSDate*)date fromDate:(NSDate*)start
{
double days = [self daysBetweenStart:start end:date];
if ((int) days % 23 == 0)
return 100.0;
else
return 0.0;
}
This function returns 100.0 if the given date is between 23 and 24 days from the start, and 0.0 otherwise. You could substitute 23 for whatever period you like. I'm not sure if this is what you wanted, so clarify your question if it wasn't.
Disclaimer: This is Cocoa. Hopefully it's the same as iPhone Cocoa?