简体   繁体   中英

Convert hh:mm time to decimal time (Matlab)

I am trying to obtain the time of sunrise and sunset in decimal hours using Matlab. Please see the following code. I already got to the point where the result is 2 1x2 "cell" arrays with time indicated as "hh:mm" in the first cell. I would like to convert it to decimal time, for example '08:41' to 8.68333 as a double array. How can I do this?

Thanks.

clear all;
close all;
tic;

startDate = datetime(date, 'ConvertFrom', 'yyyymmdd', ...
                     'TimeZone', 'UTC', ...
                     'Format', 'd MM yyyy');

%% Download sunset and sunrise time 

date_day = day(startDate);
date_month = month(startDate);
date_year = year(startDate);

% Brussels

fullURL1 = sprintf('http://api.aladhan.com/v1/calendarByCity?city=Brussels&country=Belgium&method=2&month=%02d&year=%04d',date_month,date_year);
api = fullURL1;
url = [api 'country'];
S = webread(url);

sunrise = sprintf('S.data(%02d).timings.Sunrise',date_day);
sunrise = eval(sunrise);
sunrise = strsplit(sunrise);
sunset = sprintf('S.data(%02d).timings.Sunset',date_day);
sunset = eval(sunset);
sunset = strsplit(sunset);

clearvars url fullURL1 api S

you can cast to datenum , take day-fraction from that and multiply by 24. Ex:

f = rem(datenum('08:41', 'HH:MM'), 1)*24 % format is optional
%f =
%    8.6833

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM