How to add error bars to a timeseries plot using datetime values

19 visualizaciones (últimos 30 días)
Hi all,
Please can someone help me figure out how to add error bars to my timeseries! I am gaving a problem figuring out what the x axis values should be. Would be much appreciated. (I'm new to Matlab!)
Context
I have a timeseries plot that runs Dec 17th - May 30th (alternate days). This is the code for my timeseries plot:
ts1 = timeseries(SSS_SGeast_anom_av_series, 1:2:166);
ts1.Name = ('Average salinity anomaly (psu)');
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '17-Dec-2020'; % Set start date.
ts1.TimeInfo.Format = 'dd mmm yy'; % Set format for display on x-axis.
ts.TimeInfo.Increment = '2';
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1, '- k .', 'MarkerSize', 10, 'Linewidth', 2);
hold on
My standard deviation vector is a double numeric vector, size 83 x 1. I want to add this data as error bars on to my timeseries plot, using the command:
I have set y to by the timeseries y values (SSS_SGeast_anom_av_series), and err to be the standard deviation vector mentioned above.
Problem
I can't figure out what x values I need to suit the errorbar command above?
I think I need to somehow extract the datetime values from my original timeseries and then use these, but I can't figure out how to get those values.
Either that or make a new vector of the date values, and convert this to datetime. I'm not sure.
Please help if you can! Or let me know if I've been unclear.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 14 de Jul. de 2021
Editada: Cris LaPierre el 14 de Jul. de 2021
Wouldn't your X be ts1.Time? See the Plot Datetime Values with Error Bars example for more.
I have formatted errorbar to not draw a line connecting the data points. Technically, the marker isn't necessary either, but I wanted to keep this example simple.
% Create a data set
y = randn(83,10);
SSS_SGeast_anom_av_series = mean(y,2);
ts1 = timeseries(SSS_SGeast_anom_av_series, 1:2:166);
ts1.Name = ('Average salinity anomaly (psu)');
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '17-Dec-2020'; % Set start date.
ts1.TimeInfo.Format = 'dd mmm yy'; % Set format for display on x-axis.
ts.TimeInfo.Increment = '2';
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1, '- k .', 'MarkerSize', 10, 'Linewidth', 2);
hold on
errorbar(ts1.Time,ts1.Data,std(y,[],2),'k.')
hold off
  7 comentarios
Mairead Smith
Mairead Smith el 14 de Jul. de 2021
Thanks so much Cris, this is really helpful (and useful in helping me improve in general).
However it's still not working for me - still getting errors related to the errorbar command line.
Is there any chance something is different in the version I am using?? I'm using R2020a.
I will keep trying tomorrow and maybe I'll spot something I'm missing with a fresh eye.
Thanks again.
Cris LaPierre
Cris LaPierre el 14 de Jul. de 2021
Possibly, but without knowing what the error message is, I wouldn't want to say. What happens if you copy and paste the code I've shared? Does that run without error?
Consider saving your variables to a mat file and attaching it to your post using the paperclip icon. Also copy/paste the code you are running (all the code needed to reproduce the error), as well as the full error message (all the red text).

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 14 de Jul. de 2021
Maybe you should try to employ datenum(), e.g.:
x= datenum(ts1); % See help doc how to get datenum()
errorbar(x,y,ERR);
How to use datenum(): https://www.mathworks.com/help/matlab/ref/datenum.html#d123e278633
  2 comentarios
Mairead Smith
Mairead Smith el 14 de Jul. de 2021
Thanks for the answer! Unfortunately that didn't seem to work though. It gave me this error message:
Error in CODE_Calculate_SGeast_SSSanom_std_dev (line 107)
x3 = datenum(ts1); % See help doc how to get datenum()
Caused by:
Error using datevec (line 109)
The input to DATEVEC was not an array of character vectors or strings.
Error using datenum (line 188)
DATENUM failed.
Mairead Smith
Mairead Smith el 14 de Jul. de 2021
However, I have now managed to create the datetime variable that I need: an 83 x 1 datetime vector that basically matches the x axis in my timeseries plot exactly. It's in the same datetime format, here is an extract:
'17-Dec-2020'
'19-Dec-2020'
'21-Dec-2020'
'23-Dec-2020'
'25-Dec-2020'
But it still doesn't accept this within the errorbar command! This is the error message I see when I run the command e = errorbar(x, SSS_SGeast_anom_av_series, err);
Error using errorbar (line 76)
Input arguments must be numeric or objects which can be converted to double.
Error in CODE_Calculate_SGeast_SSSanom_std_dev (line 119)
e = errorbar(x,SSS_SGeast_anom_av_series, err);
So I tried converting the datetime vector into a double precision numerical array (following these instructions https://www.mathworks.com/help/matlab/matlab_prog/convert-between-datetime-arrays-numbers-and-strings.html) and it still didn't accept it. This is the error message I saw that time:
Warning: Error updating ErrorBar.
Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS,
MINUTES, HOURS, DAYS, or YEARS functions.
I have no idea if it wants double, or datetime, or what!?

Iniciar sesión para comentar.

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by