How to add a vector of seconds to time HH:mm:ssPM in MATAB?
I usually have this nice way in Excel to convert normal number format to hour and minutes and sec. format using simple cell custom formatting, but when I put down code below in MATLAB, instead of incrementing in seconds, it adds in days!
time = 1+0:50000+0; % sec
% To show date as plot label it should be converted from numbers to letters
hr_matlab = time' + datenum('4:10:44 PM');
hr= datestr(hr_matlab, 'HH:MM:ssPM');
figure(222)
plot(hr,S,'-b','LineWidth',2)
I am using MATLAB2014a and don't have access to function datetime
.
datenum
converts the date to a number that represents days as whole numbers. For that reason, when you add the vector [1,2,3,...], you acturally add days to your fixed time ('4:10:44 PM')
.
if you want to add it as seconds, you need to divide time
in the amount of seconds per day:
hr_matlab = (time')/86400 + datenum('4:10:44 PM');