How to collect datetime from text entry and use in plot?
3 ビュー (過去 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?
0 件のコメント
採用された回答
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 件のコメント
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 Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!