How to collect datetime from text entry and use in plot?

5 次查看(过去 30 天)
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');
disp(startD);
disp(finishD);
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
156)
tstart = datetime(startD);
What exactly should I change to get the data plotted according to the input datetime?

采纳的回答

dpb
dpb 2018-8-4
编辑:dpb 2018-8-4
Almost there...you'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 =
datetime
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?
  2 个评论
Chamath Vithanawasam
Hey, thanks a lot! This worked perfectly. And regarding the calendar control, I just checked, and I think there is such a feature. https://www.mathworks.com/help/finance/uicalendar.html Perhaps I will incorporate that into my code.
F S
F S 2020-6-17
there is also a uidatepicker function, in case you don't have the Finance tool box. What I miss in both these functions is that it lets you pick the date only, and not also the time.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by