将 hh:mm 时间转换为十进制时间(Matlab) [英] Convert hh:mm time to decimal time (Matlab)

查看:68
本文介绍了将 hh:mm 时间转换为十进制时间(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Matlab 以十进制小时获取日出和日落时间.请看下面的代码.我已经到了结果是 2 1x2 单元格"的地步.时间表示为hh:mm"的阵列;在第一个单元格中.我想将其转换为十进制时间,例如 '08:41' 到 8.68333 作为双数组.我该怎么做?

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?

谢谢.

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

推荐答案

你可以投射到 datenum,从中取天分数并乘以 24.例如:

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

这篇关于将 hh:mm 时间转换为十进制时间(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆