Start and end date picker to load data to graph
6 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(3 个)
Star Strider
2022-9-1
2 个评论
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'})
Lv = isbetween(UCIWEATHER{:,1}, "01-Feb-2021","07-Feb-2021");
Feb2021 = UCIWEATHER(Lv,:)
Experiment with the intervals you want to explore.
.
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
2022-9-10
Attached the code for your reference. You may modify it according to your needs.
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
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
load UCIWEATHER.mat
tt = timetable(UCIDAYS,UCIHIGH,UCILOW,UCIAVG)
Use timerange
Extracting a range of row-times from a timetable is easy with timerange.
startTime = datetime(2021,1,4)
endTime = datetime(2021,10,10)
ttPlot = tt(timerange(startTime,endTime),:)
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";
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polar Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!