Scale datetime to the length of longer array

t1 = datetime([2023,03,03,10,00,0]);
t10 = datetime([2023,03,28,11,00,0]);
tstep = days(1);
T1 = t1:tstep:t10;
T1 = T1(1:length(FLWAll));
This is my datetime function that I can't get working. FLWAll is an 2500x1 long array which I want to plot against my datetime (T1). If i'm using tstep = seconds() or minutes() I can get the same length for my datetime and FLWAll, but I'd like to rescale the datetime "array" so that It shows days instead. Using tstep = days(1) I get a 1x26 datetime, which is way to short.
TLDR; I'm plotting FLWAll that is data ranging from 03-03/2023 to 28-03/2023, and I'd like to show a datetime as my X-axis, where the steps are in days.

1 Comment

You need to provide some more information.
Your variable FLWAll is 2500x1 but what with what time-step?
Your total delta time is of 601 hours between t1 and t10, are the 2500 values linearly distributed over this interval?

Sign in to comment.

 Accepted Answer

There are 26 days between 3-3 and 28-3. That is what a step of 1 day will give you.
I would format the xticks to display in your preferred format, but create the vector in a way that makes it easy to get the increment you need.
FLWAll = rand(2500,1);
t1 = datetime([2023,03,03,10,00,0]);
t10 = datetime([2023,03,28,11,00,0]);
tstep = (t10-t1)/(length(FLWAll)-1);
T1 = t1:tstep:t10;
plot(T1,FLWAll)
% create tick for each day
xticks(t1:days(1):t10);
xtickformat('dd')

6 Comments

Thank you so much, this did the trick!
A follow up (and more complicated) question would be this if you or anyone else could help me. The bigger array FLWAll is made from data gathered over multiple days, but the length of the different smaller arrays are never the same length (as seen below).
  • The main goal would be to make the xticks match the lengths of the smaller arrays (FLW1,2,3 etc.) and also with their corresponding date.
This would just be a bonus, I'd be fine using the method you gave me, but this would take the plot to the next level (imho).
FLW1 = rand(1,50); % data collected 03/03
FLW2 = rand(1,125); % data collected 06/03
FLW3 = rand(1,75); % data collected 11/03
FLWAll = [FLW1,FLW2,FLW3];
t1 = datetime([2023,03,03,10,00,0]);
t10 = datetime([2023,03,28,11,00,0]);
tstep = (t10-t1)/(length(FLWAll)-1);
T1 = t1:tstep:t10;
plot(T1,FLWAll)
% create tick for each day
xticks(t1:days(1):t10);
xtickformat('dd')
If you plot using the dates they were collected, you will end up with gaps between each data set. There is also an unknown still, as you do not specify the end date of the individual vectors. You can't create the x vector without knowing that.
Thanks for your reply again. Gaps would not look very nice, that's true. Is it maybe possible to plot them using a regular vector or xticks that follows each data set, and then just add the dates afterwards?
So to specify the end dates would that be something like this:
FLW1 = rand(1,50); % data collected 03/03
FLW2 = rand(1,125); % data collected 06/03
FLW3 = rand(1,75); % data collected 11/03
FLWAll = [FLW1,FLW2,FLW3];
t1 = datetime([2023,03,03,10,00,0]); % Starttime
t2 = datetime([2023,03,06,10,00,0]); % End/starttime for the next dataset
t3 = datetime([2023,03,11,10,00,0]); % End/starttime for the next dataset
t10 = datetime([2023,03,28,11,00,0]);% Endtime for it all
tstep = (t10-t1)/(length(FLWAll)-1);
T1 = t1:tstep:t10;
% plot(T1,FLWAll)
% I don't get this to work, but I would assume it's something like this. Or
% maybe i'm completly wrong
xticks([t1:days(1):t2],[t2:days(1):t2],[t3:days(1):t10]);
Error using xticks
Too many input arguments.
xtickformat('dd')
Ah, maybe it wouldn't look as bad as I thought. I am used to dates being mm-dd but you are using dd-mm.
It is more common to have the line somehow indicate the different data series. The X axis typically has uniform spacing across all datasets plotted on it.
For a first attempt, why not plot your 3 data sets as 3 separate lines?
FLW1 = rand(1,50); % data collected 03/03
FLW2 = rand(1,125); % data collected 06/03
FLW3 = rand(1,75); % data collected 11/03
FLWAll = [FLW1,FLW2,FLW3];
t1 = datetime([2023,03,03,10,00,0]); % Starttime
t2 = datetime([2023,03,06,10,00,0]); % End/starttime for the next dataset
tstep1 = (t2-t1)/(length(FLW1)-1);
T1 = t1:tstep1:t2;
t3 = datetime([2023,03,11,10,00,0]); % End/starttime for the next dataset
tstep2 = (t3-t2)/(length(FLW2)-1);
T2 = t2:tstep2:t3;
t10 = datetime([2023,03,28,11,00,0]);% Endtime for it all
tstep3 = (t10-t3)/(length(FLW3)-1);
T3 = t3:tstep3:t10;
plot(T1,FLW1,T2,FLW2,T3,FLW3)
% create tick for each day
xticks(t1:days(1):t10);
xtickformat('dd')
Axel O
Axel O on 1 May 2023
Edited: Axel O on 1 May 2023
Sorry for the late reply.
That's an easy solution to the problem that I looked past. In my main files I've got 15 datasets that I summed to the FLWAll array to make things easier to the eye, but now that all the coding is done and I just need to update the visuals, this solution will do just fine! Might look a bit messy but it's working the exact way I want.
Thanks for all your help by the way!
EDIT: Tried using this method above with the gaps in the dates, and it looks horrific. I think I'll stick to using a linear datetime, even though the dates don't match up.
You can set the linespec if you want, but unless your sampling frequency is constant, you can't just arbitrarly combine data sets.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!