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

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

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

when I use 'm' for month I get this: Warning: The format 'dd-mm-yyyy' contains a field for minute (m) in what appears to be a date portion. You might have intended to use the symbol for month (M) rather than for minute (m). See the datetime.Format property for a complete description of the identifiers used in format strings.
But thanks for your answer, it works. Do you know a way to increase the amount of ticks (it now only does each month and with the simple plot/datetick combination it does a lot more ticks)?
Interesting about the warning you get for 'dd-mm-yyyy', because I didn’t get it when I tested the code I posted (that runs without error, as does the code here, written and run with R2015b).
This sets weekly x-ticks (in the ‘new_ticks’ assignment), then reduces the font size and rotates them so they’re readable. You can set any number of ticks you want:
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')
tks = get(gca, 'XTick'); % Get Current Number Of Ticks
new_tks = linspace(min(tks), max(tks), 4*length(tks)); % Ticks For Every Week
set(gca, 'XTick', new_tks, 'FontSize',8, 'XTickLabelRotation',45) % Set Ticks, Reduce Font Size, Rotate
The ability to rotate the tick labels is new as of the last few releases (I believe R2014b), so if my code throws an error on 'XTickLabelRotation', just edit it out.
Thanks man
As you can see the date repeats every 4 ticks. Shouldn't it be over the full length? I know that your new_tks is 16 in size so that is not the problem. But what is? Is it a bug?
Sidenote; I think Matlab has a bug because I now use MM as Month in the datetime function and mm for Month in all of the other functions
adding this fixes the last issue: set(gca, 'XTickLabel', datestr(new_tks, 'dd-mm-yyyy'))

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

Je
on 19 Nov 2015

Commented:

on 20 Nov 2015

Community Treasure Hunt

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

Start Hunting!