How to reset military datetime in matlab?

2 ビュー (過去 30 日間)
Wiktoria Glogowska
Wiktoria Glogowska 2019 年 8 月 14 日
回答済み: Motasem Mustafa 2020 年 10 月 23 日
Hello,
I have a problem with the way that Matlab reads user time input from the gui. When user inputs 07-01-2019 00:00:00 matlab reads the variables as 07-01-2019 12:00:00. Same for the loaded data timeframe. Also user cannot input any 24h military time, like for example input of : 07-01-2019 22:00:00, gives the error:
Unable to parse '01-01-2019 22:00:00' as a date/time using the format 'dd-MM-yyyy hh:mm:ss'.
I have used the command below to reset the default settings before, but not in the current script. Would that have any effect on how matlab reads the time? Any help would be appreciated.
datetime.setDefaultFormats('default','yyyy-MM-dd hh:mm:ss')

回答 (2 件)

Rik
Rik 2019 年 8 月 14 日
As you can read in the doc, the hh identifier is a 12h system. If you want 24h, you need to use HH in your format.

Motasem Mustafa
Motasem Mustafa 2020 年 10 月 23 日
I used to have the same issue aىd I have posted my question yesterday :
'' Dears,
I am using the code below to do parsing for date-time cells in an MS Excel sheet with date-time form of ( 01/05/2019 00:00) as in the screenshot below.
clc,clear,close all;
[num1,data] = xlsread('Book_new.xlsx','sheet1','A1:A30');
a=datevec(data,'dd/mm/yyyy HH:MM:SS');
date=datestr(datenum(a),'dd/mm/yyyy');
time=datestr(datenum(a),'HH:MM:SS');
Year=datestr(datenum(a),'yyyy');
mm=datestr(datenum(a),'mm');
dd=datestr(datenum(a),'dd');
yy=datestr(datenum(a),'yyyy');
[status,message] =xlswrite('motasem.xlsx',str2num(yy),'sheet1','A1:A30');
[status,message] =xlswrite('motasem.xlsx',str2num(mm),'sheet1','B1:B30');
[status,message] =xlswrite('motasem.xlsx',str2num(dd),'sheet1','C1:C30');
[status,message] =xlswrite('motasem.xlsx',string(time),'sheet1','D1:D30');
When I run the code for example for the 1st 30 readings (half hourly readings) it gives me the following error :
"Error using dtstr2dtvecmx
Failed to convert from text to date number.
Error in datevec (line 123)
y = dtstr2dtvecmx(t,icu_dtformat);
Error in motasem (line 4)
a=datevec(data,'dd/mm/yyyy HH:MM:SS');"
But when I change the range of data to avoid the first reading which contains the time 00:00:00 it works and gives the below output :
Any suggestions please ?
"
The new code that works is using readtable function as follows :
clc,clear,close all;
data = readtable('Book_new.xlsx','Range','A1:A60','ReadVariableNames',false);
A = table2array(data);
yy=datestr(datenum(A),'yyyy');
mm=datestr(datenum(A),'mm');
dd=datestr(datenum(A),'dd');
time=datestr(datenum(A),'HH:MM:SS');
[status,message] =xlswrite('motasem.xlsx',str2num(yy),'sheet1','A1:A30');
[status,message] =xlswrite('motasem.xlsx',str2num(mm),'sheet1','B1:B30');
[status,message] =xlswrite('motasem.xlsx',str2num(dd),'sheet1','C1:C30');
[status,message] =xlswrite('motasem.xlsx',string(time),'sheet1','D1:D30');
Hope this will help you
All the best

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by