1
votes

I have date & time in intervals of one hour(3600 seconds) in number format, e.g 0,3600, 7200, 10800, 14400, 18000 etc. I have starting date and time , e.g 0 corresponds to 2005/06/01 01:00 in 'yyyy/mm/dd HH:MM' format.

I am writing this data to Excel file, so I am looking for way where I can convert time given in Hour (in seconds) to Date Time (2005/06/01 01:00, 2005/06/01 02:00 etc) before writing to excel file.

I have explored 'datenum' and 'datestr' functions but they are not useful since I can not give them customised start time i.e (0 corresponds to 2005/06/01 01:00). May be if some one can help me to point me in right direction.

tempMatrix = [NrID time_inSec ff X Y];
tempMatrix_dataCell=num2cell(tempMatrix);
col_header={'NrID','Time','ff','X','Y'};
data_for_xls_file=[col_header; tempMatrix_dataCell];
xlswrite('My_file.xls',data_for_xls_file);

time_inSec is column with values 0, 3600, 7200, 10800 etc which need to be converted.

When I use datenum it returns 7.3246e+05 so when I add 3600 to get 2005/06/01 02:00 and pass it to datestr it returns 2015/04/10 01:00.

 temp_time = datenum('2005/06/01 01:00','yyyy/mm/dd HH:MM')
1
The mentioned functions datenum and datestr are useful and you can use them to do what you want. Please provide some code (minimal required) to reproduce your problem.Robert Seifert
datenum makes a number that is in units of days. Just convert your "hours" from their units of seconds to days and add them to the datenum of you dates. Then convert them to Excel date numbers using m2xdateDan
@Dan Thanks, I was working in diff unitsBasit

1 Answers

0
votes

This works with a given start time:

startTime = datenum('2005/06/01 01:00', 'yyyy/mm/dd HH:MM'); % Define start time.
currentTime = datenum('2005/06/01 02:00', 'yyyy/mm/dd HH:MM'); % Current time.
timePassedHours = (currentTime - startTime) * 24; % Time that has passed in hours.
display(timePassedHours); % Print the output.