How do I use datetime on the x-axis of the errorplot.

5 views (last 30 days)
So I have tried this with the normal plot function and then it works, but as I replace plot with errorbar it doesn't work anymore(of course I delete the extra information that is required for the error part of errorbar). This is the error I get when using datetime with errorbar: error using datetime/double (line 1468). Undefined function 'double' for input arguments of type 'datetime'.
Here is a bit of that code:
datums = ['25-09-2015'; '01-10-2015'; '06-10-2015'; '08-10-2015'; '15-10-2015'; '20-10-2015'; '21-10-2015'; '26-10-2015'; '27-10-2015';'28-10-2015'];
datums1 = datetime(datums,'Format','dd-MM-yyyy');
piek1 = [-4;-5;-6;-8;-1;-2;-4;-3;-4;-5];
error1 = [0.3960; 0.5300; 0.4830; 0.1800; 0.2330; 0.4210; 0.5910; 1.0170; 1.6480; 0.7370];
figure(1);
errorbar(datums1,piek1, error1) % gives error
% or plot(datums1,piek1) % this one does work correctly and as I want it to
Is there a way to plot datetime data on the x-axis of a errorbar plot (and still have it be in automatic mode)?

Accepted Answer

Star Strider
Star Strider on 19 Nov 2015
Some plot functions don’t work and play well with datetime objects for some reason. A work-around is to use the datenum and datetick functions:
datums = ['25-09-2015'; '01-10-2015'; '06-10-2015'; '08-10-2015'; '15-10-2015'; '20-10-2015'; '21-10-2015'; '26-10-2015'; '27-10-2015';'28-10-2015'];
datnms = datenum(datums,'dd-mm-yyyy');
piek1 = [-4;-5;-6;-8;-1;-2;-4;-3;-4;-5];
error1 = [0.3960; 0.5300; 0.4830; 0.1800; 0.2330; 0.4210; 0.5910; 1.0170; 1.6480; 0.7370];
figure(1)
errorbar(datnms,piek1, error1) % gives error
datetick('x', 'dd-mm-yyyy')
You also had an error in your date format. The two-digit ‘month’ format is 'mm', minutes is 'MM'. (I fixed those here.)
  5 Comments
Je
Je on 20 Nov 2015
adding this fixes the last issue: set(gca, 'XTickLabel', datestr(new_tks, 'dd-mm-yyyy'))

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!