MATLAB: How to collect datetime from text entry and use in plot


I have a collection of data that is taken at specific times. I plot this data in the usual method by using plot(). I need to make changes to the x-axis limits so I use xlim() and get the two limits as datetime()
tstart = datetime(2018,02,01,15,30,00);
tend = datetime(2018,02,06,15,30,00);
xlim([tstart tend]);
Suppose I want to get this dates and times from a text entry in a GUI. Namely, the 'edit text' feature. How do I type the start time and end time to two different 'edit text's and use it in the variables 'tstart' and 'tend'? I used the following technique axes(handles.axes1); plot((a{:,1}),(a{:,listValueAA+1}));
startD = get(handles.edit2,'String');
finishD = get(handles.edit3,'String');
tstart = datetime(startD);
tend = datetime(finishD);
xlim([tstart tend]);
But I get the following error.
Error using datetime (line 614)
Could not recognize the date/time format of
'2018,02,03,15,00,00'. You can specify a format
character vector using the 'InputFormat'
parameter. If the date/time text contains day,
month, or time zone names in a language foreign
to the 'en_US' locale, those might not be
recognized. You can specify a different locale
using the 'Locale' parameter.
Error in GUI_for_log>pushbutton1_Callback (line
tstart = datetime(startD);
What exactly should I change to get the data plotted according to the input datetime?

Best Answer

  • Almost're trying to convert the string containing the date vector representation, but datetime doesn't know that input syntax --
    Convert the string to a datevec:
    >> datetime(str2num('2018,02,03,15,00,00'))
    ans =
    03-Feb-2018 15:00:00
    or, in your input routine
    startD = str2num(get(handles.edit2,'String'));
    finishD = str2num(get(handles.edit3,'String'));
    Alternatively ask your user to input the date as a conventional date string; of course it needs to be in recognizable format and correct order...I don't do GUIs other than most rudimentary uigetfile or the like occasionally; don't suppose TMW has packaged a calendar control that you could just call directly have they?