Start and end date picker to load data to graph

4 views (last 30 days)
Hi there, I am currently working on a project where I want to use two data pickers, one as a start date and one as the end date. I have a data set from the weather in a city in 2021 (don't know if its important but the data set is as datetime), I want to be able to select a start and end date and plot the data in a graph, I already have the code to plot the entire year but want to be able to for example just chose the data from Apirl 1st to october 1st and plot it with a button, but I can't figure how to get the data picker to work. I don't know if I made my self clear of what I am asking for. Thank you!

Answers (3)

Star Strider
Star Strider on 1 Sep 2022
The core MATLAB between function (R2014b+) may do what you want.
  2 Comments
Star Strider
Star Strider on 1 Sep 2022
Edited: Star Strider on 8 Sep 2022
The isbetween function returns a logical vector, so use that as the row (first) argument in any matrix or table address reference (assuming the datetime vector is a column vector and the other data are oriented similarly).
EDIT — (8 Sep 2022 at 20:10)
Try this —
LD = load(websave('UCIWEATHER','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1120270/UCIWEATHER.mat'));
UCIWEATHER = table(LD.UCIDAYS, LD.UCIHIGH, LD.UCILOW, LD.UCIAVG, 'VariableNames',{'UCIDAYS','UCIHIGH','UCILOW','UCIAVG'})
UCIWEATHER = 365×4 table
UCIDAYS UCIHIGH UCILOW UCIAVG ___________ _______ ______ ______ 01-Jan-2021 66.272 45.806 78.942 02-Jan-2021 66.452 46.22 79.446 03-Jan-2021 66.92 45.842 79.302 04-Jan-2021 66.758 45.572 78.951 05-Jan-2021 66.65 46.256 79.581 06-Jan-2021 66.596 47.192 80.49 07-Jan-2021 67.316 47.948 81.606 08-Jan-2021 66.56 48.344 81.624 09-Jan-2021 66.902 48.416 81.867 10-Jan-2021 66.632 48.38 81.696 11-Jan-2021 67.262 48.092 81.723 12-Jan-2021 66.722 48.29 81.651 13-Jan-2021 68.324 48.128 82.29 14-Jan-2021 68.504 48.254 82.506 15-Jan-2021 68.054 49.064 83.091 16-Jan-2021 67.28 49.388 83.028
Lv = isbetween(UCIWEATHER{:,1}, "01-Feb-2021","07-Feb-2021");
Feb2021 = UCIWEATHER(Lv,:)
Feb2021 = 7×4 table
UCIDAYS UCIHIGH UCILOW UCIAVG ___________ _______ ______ ______ 01-Feb-2021 67.298 47.408 81.057 02-Feb-2021 66.722 48.542 81.903 03-Feb-2021 66.902 48.884 82.335 04-Feb-2021 67.082 48.632 82.173 05-Feb-2021 66.434 49.748 82.965 06-Feb-2021 66.416 49.604 82.812 07-Feb-2021 66.2 49.208 82.308
Experiment with the intervals you want to explore.
.

Sign in to comment.


Simon Chan
Simon Chan on 2 Sep 2022
If your weather data are arranged in timetable format, another option you may use is function timerange.
Suppose the variable name is "rawdata" with VariableName "Date" containing all datetime of your data.
Define two uidatepickers, dp1 (Start date) and dp2 (End date) as follows:
dp1 = uidatepicker(f,'Position',[xx xx xx xx]); % 1st uidatepicker (You may need to define the position)
dp1.ValueChangedFcn = @updateData; % Callback for update the selected date
dp2 = uidatepicker(f,'Position',[xx xx xx xx]); % 2nd uidatepicker
dp2.ValueChangedFcn = @updateData;
firstdate = rawdata.Date(1); % Find the first date of your data (provided they are already sorted)
lastdate = rawdata.Date(end); % Find the last date of your data
set(dp1,'Limits',[firstdate, lastdate]); % Set limits for datepicker
set(dp2,'Limits',[firstdate, lastdate]); % Set limits for datepicker
%
function updateData(src,event)
if ~isnat(dp1.Value) && ~isnat(dp2.Value)
if dp2.Value>dp1.Value
S = timerange(dp1.Value,dp2.Value,'closed');
T = rawdata(S,:); % Variable T is the extracted data for you to plot the requried data
%% Add the code to plot the data here
end
end
end
  4 Comments
Simon Chan
Simon Chan on 10 Sep 2022
Attached the code for your reference. You may modify it according to your needs.

Sign in to comment.


Seth Furman
Seth Furman on 12 Sep 2022
Edited: Seth Furman on 12 Sep 2022
Use a timetable
As others have mentioned, we can work with this data much more easily by using a timetable.
!wget https://in.mathworks.com/matlabcentral/answers/uploaded_files/1120270/UCIWEATHER.mat
--2022-09-12 15:25:34-- https://in.mathworks.com/matlabcentral/answers/uploaded_files/1120270/UCIWEATHER.mat Resolving in.mathworks.com (in.mathworks.com)... 96.6.120.90 Connecting to in.mathworks.com (in.mathworks.com)|96.6.120.90|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 6941 (6.8K) [application/octet-stream] Saving to: ‘UCIWEATHER.mat’ UCIWEATHER.mat 0%[ ] 0 --.-KB/s UCIWEATHER.mat 100%[============================================================================================================>] 6.78K --.-KB/s in 0s 2022-09-12 15:25:34 (120 MB/s) - ‘UCIWEATHER.mat’ saved [6941/6941]
load UCIWEATHER.mat
tt = timetable(UCIDAYS,UCIHIGH,UCILOW,UCIAVG)
tt = 365×3 timetable
UCIDAYS UCIHIGH UCILOW UCIAVG ___________ _______ ______ ______ 01-Jan-2021 66.272 45.806 78.942 02-Jan-2021 66.452 46.22 79.446 03-Jan-2021 66.92 45.842 79.302 04-Jan-2021 66.758 45.572 78.951 05-Jan-2021 66.65 46.256 79.581 06-Jan-2021 66.596 47.192 80.49 07-Jan-2021 67.316 47.948 81.606 08-Jan-2021 66.56 48.344 81.624 09-Jan-2021 66.902 48.416 81.867 10-Jan-2021 66.632 48.38 81.696 11-Jan-2021 67.262 48.092 81.723 12-Jan-2021 66.722 48.29 81.651 13-Jan-2021 68.324 48.128 82.29 14-Jan-2021 68.504 48.254 82.506 15-Jan-2021 68.054 49.064 83.091 16-Jan-2021 67.28 49.388 83.028
Use timerange
Extracting a range of row-times from a timetable is easy with timerange.
startTime = datetime(2021,1,4)
startTime = datetime
04-Jan-2021
endTime = datetime(2021,10,10)
endTime = datetime
10-Oct-2021
ttPlot = tt(timerange(startTime,endTime),:)
ttPlot = 279×3 timetable
UCIDAYS UCIHIGH UCILOW UCIAVG ___________ _______ ______ ______ 04-Jan-2021 66.758 45.572 78.951 05-Jan-2021 66.65 46.256 79.581 06-Jan-2021 66.596 47.192 80.49 07-Jan-2021 67.316 47.948 81.606 08-Jan-2021 66.56 48.344 81.624 09-Jan-2021 66.902 48.416 81.867 10-Jan-2021 66.632 48.38 81.696 11-Jan-2021 67.262 48.092 81.723 12-Jan-2021 66.722 48.29 81.651 13-Jan-2021 68.324 48.128 82.29 14-Jan-2021 68.504 48.254 82.506 15-Jan-2021 68.054 49.064 83.091 16-Jan-2021 67.28 49.388 83.028 17-Jan-2021 66.848 49.154 82.578 18-Jan-2021 67.082 49.154 82.695 19-Jan-2021 66.686 48.902 82.245
For simple visualizations, use stackedplot
Simple visualizations can be created with stackedplot.
sp = stackedplot(ttPlot,{["UCIHIGH","UCILOW","UCIAVG"]},"Title","Average Temperature","DisplayLabels","Temperature","XLabel","Days");
sp.LineProperties(1).Color = [1 0 0; 0 0 0; 0 0 1];
sp.LineProperties(1).MarkerEdgeColor = [1 0 0; 0 0 0; 0 0 1];
sp.LineProperties(1).LineStyle = {'none','none','-',};
sp.LineProperties(1).Marker = {'o','+','none'};
sp.AxesProperties(1).LegendVisible = "off";

Categories

Find more on Timetables 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!

Translated by