Datime User Input Problem

29 views (last 30 days)
Wiktoria Glogowska
Wiktoria Glogowska on 13 Aug 2019
Commented: Steven Lord on 13 Aug 2019
I am designing an app where user has to input the start and end time for the data to be displayed. When the editbox is updated the date format is what the user has chosen, however the way start and end dates are saved in matlab is incorrect i.e. days become month and month decome days (1st of June becomes 6th of Jan) . This is the prompt that I am using for the input window and next is how I convert the dataset time variable which is datenumer into a 'readable' format. I would appreciate if someone could help me to figure it out? (I would like to stick to European datetime format.)
prompt = {'Enter start time (format: dd-MM-yyyy hh:mm:ss):','Enter end time: (format: dd-MM-yyyy hh:mm:ss)','Enter the length of time window (in Hours):','Enter the length of overlap (in Hours):'};
Start= datenum(answer(1,1));
Starttime=datetime(Start,'ConvertFrom','datenum', 'InputFormat', 'dd-MM-yyyy hh:mm:ss');

Answers (1)

Guillaume
Guillaume on 13 Aug 2019
The problem is because of your unnecessary transition through datenum. When you do this:
Start= datenum(answer(1,1));
matlab interprets the ambiguous date input with the 'mm-dd-yyyy ...' (datenum) format , so, at this point your date is already stored wrong. Note that in the next line:
Starttime=datetime(Start,'ConvertFrom','datenum', 'InputFormat', 'dd-MM-yyyy hh:mm:ss');
the 'InputFormat' has no effect since a datenum doesn't have a format. A datenum can only be interpreted one way.
You could fix it by telling matlab how to convert the text to datenum:
Start = datenum(answer{1}, 'dd-mm-yyyy HH:MM:SS'); %proper to acess answer. Note that datenum format syntax differs from datetime format syntax.
But the simplest thing is to get rid of that unnecessary transition through datenum:
Starttime = datetime(answer{1}, 'InputFormat', 'dd-MM-yyyy hh:mm:ss');
Finally, note that accessing the content of cell arrays is done with {} not (), and when indexing vectors linear indexing is safer and simpler than 2D indexing. Hence, answer{1} instead of answer(1, 1).
  3 Comments
Guillaume
Guillaume on 13 Aug 2019
However I do need the datenum for some conditional statements
Whatever you're doing with the datenum you can do with datetime in a safer way. There's never any reason to use datenum in modern matlab. datetime does everything it could do, is safer, and more powerful.
Numeric input data must be a matrix with three or six columns
You're obviously varying from my answer since I'm suggesting passing the char vector answer{1} to datetime, not numeric data. If you're converting from datenum, you still need the 'ConvertFrom', 'datenum' (but not the 'InputFormat' part which has no effect).
I would want 01-01-2019.
The display format is a different property from the input format. You can either specify the exact display format that you want with the 'Format' attribute:
Starttime = datetime(answer{1}, 'InputFormat', 'dd-MM-yyyy hh:mm:ss', 'Format', 'dd-MM-yyyy hh:mm:ss');
but it's annoying to have to specify the format twice, so instead you can tell 'Format' to use the input format:
Starttime = datetime(answer{1}, 'InputFormat', 'dd-MM-yyyy hh:mm:ss', 'Format', 'preserveinput');
Steven Lord
Steven Lord on 13 Aug 2019
However I do need the datenum for some conditional statements
I agree with Guillaume's assertion that you can likely just replace datenum with datetime even in your conditional code. For example datetime works with the relational operators:
T = datetime('today');
T < (T + days(1)) % True, today is earlier than tomorrow
T < (T - days(1)) % False, today is not earlier than yesterday
and many other operations. If you show us a small example of the conditional statement that you believe needs to be done on the serial date numbers, we can try to show you how that would work with datetime arrays.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!