Start and end date picker to load data to graph

조회 수: 22 (최근 30일)
Fortino
Fortino 2022년 9월 1일
편집: Aditya 2024년 3월 5일
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!

답변 (3개)

Star Strider
Star Strider 2022년 9월 1일
The core MATLAB between function (R2014b+) may do what you want.
  댓글 수: 2
Fortino
Fortino 2022년 9월 1일
How could I use that function with two date pickers? Also, how could I add the data from the weather, it seems that the function might be able to work, im just not sure on how to create it.
Star Strider
Star Strider 2022년 9월 1일
편집: Star Strider 2022년 9월 8일
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.
.

댓글을 달려면 로그인하십시오.


Simon Chan
Simon Chan 2022년 9월 2일
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
  댓글 수: 5
Simon Chan
Simon Chan 2022년 9월 10일
Attached the code for your reference. You may modify it according to your needs.
Aditya
Aditya 2024년 3월 5일
편집: Aditya 2024년 3월 5일
Canyou help me out i am having the same problem. I have attached the excel file i want to also plot this data the same way using datepicker. Please help me out with the code its gonna be data for 5 years total i am getting error while using it as an excel file. I want to load this data , in uitable and then using date picker want to plot it.

댓글을 달려면 로그인하십시오.


Seth Furman
Seth Furman 2022년 9월 12일
편집: Seth Furman 2022년 9월 12일
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";

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by