Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
put date in x axis when using plotyy

Subject: put date in x axis when using plotyy

From: joanna

Date: 11 Jul, 2014 19:57:17

Message: 1 of 27

I have a file consisting 10 columns of which columns 3-5 are dates in the form dd-mm-yyyy.
I use plotyy command:

a1=filename(:,2)
a2=filename(:,9)
[yaxes,a1,a2]=plotyy(filename(:,1),a1,filename(:,1),a2,'plot','stem')
%set axes labels and properties
set(yaxes(2),'ylim',[5.5 7]
set(yaxes(2),'ytick,[5.5:0.5: 7]
set(yaxes(1),'box','off')
axes(yaxes(1));
ylabel('y1')
axes(yaxes(1));
ylabel('a2')
title('plotdata')
set(a2,'colour','r')
set(a2,'marker','*')

,where filename(:,1) consist serial numbers and is my x axis.
What i want to do is replace filename(:,1) with filename(:,3:5) (i.e my dates) in the above form (dd-mm-yyyy).
I've tried using detevec,datenum,datestr and datetick but none of them is working right..
Can anyone help me with that?

Subject: put date in x axis when using plotyy

From: dpb

Date: 11 Jul, 2014 21:19:59

Message: 2 of 27

On 07/11/2014 2:57 PM, joanna wrote:
> I have a file consisting 10 columns of which columns 3-5 are dates in
> the form dd-mm-yyyy.
> I use plotyy command:
>
> a1=filename(:,2)
> a2=filename(:,9)
> [yaxes,a1,a2]=plotyy(filename(:,1),a1,filename(:,1),a2,'plot','stem')
> %set axes labels and properties
> set(yaxes(2),'ylim',[5.5 7]
> set(yaxes(2),'ytick,[5.5:0.5: 7]
> set(yaxes(1),'box','off')
> axes(yaxes(1));
> ylabel('y1')
> axes(yaxes(1));
> ylabel('a2')
> title('plotdata')
> set(a2,'colour','r')
> set(a2,'marker','*')
>
> ,where filename(:,1) consist serial numbers and is my x axis.
> What i want to do is replace filename(:,1) with filename(:,3:5) (i.e my
> dates) in the above form (dd-mm-yyyy).
> I've tried using detevec,datenum,datestr and datetick but none of them
> is working right..
> Can anyone help me with that?

This is most peculiar -- why on earth are you using the variable
"filename" for the data??? Anyway, that aside, presuming the first
column is, indeed a Matlab datenum, then

   dn=filename(:,1); % a shorter, more mnemonic name for the datenum
  hAx=plotyy(x,a1,x,a2,@plot,@stem);
  set(hAx(2),'xtick',[]) % hide the RH x-axis labels so don't see'um
  datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')

If you need to change the x-axes xlim, then need to do that _before_
calling datetick and then you need to be sure to do the same thing to
_both_ axes to keep them in synch. Here since didn't change the range,
all one needs do is to convert the LH to dates and hide the RH so they
don't fight each other.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 00:04:25

Message: 3 of 27

On 07/11/2014 4:19 PM, dpb wrote:
...

> dn=filename(:,1); % a shorter, more mnemonic name for the datenum
> hAx=plotyy(x,a1,x,a2,@plot,@stem);

ERRATUM: Switched later; missed changing 'x' to 'dn'...
  hAx=plotyy(dn,a1,dn,a2,@plot,@stem);

> set(hAx(2),'xtick',[]) % hide the RH x-axis labels so don't see'um
> datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')

...

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 12 Jul, 2014 07:10:18

Message: 4 of 27

dpb <none@non.net> wrote in message <lppu18$1i5$1@speranza.aioe.org>...
> On 07/11/2014 4:19 PM, dpb wrote:
> ...
>
> > dn=filename(:,1); % a shorter, more mnemonic name for the datenum
> > hAx=plotyy(x,a1,x,a2,@plot,@stem);
>
> ERRATUM: Switched later; missed changing 'x' to 'dn'...
> hAx=plotyy(dn,a1,dn,a2,@plot,@stem);
>
> > set(hAx(2),'xtick',[]) % hide the RH x-axis labels so don't see'um
> > datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')
>
> ...
>
> --
Thanks for answering!
I used 'filename' as example.
The 1st column isn't a Matlab datenum,it's simple data serial numbers.
The 3rd column is days,4th is months and 5th are years.
I tried what you suggested but i got this message:

??? Error using ==> datetick>parseinputs at 444
Incorrect arguments

Error in ==> datetick at 68
[axh,nin,ax,dateform,keep_ticks,keep_limits] = parseinputs(varargin);

Error in ==> vetaplot1 at 30
datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 11:20:11

Message: 5 of 27

On 07/12/2014 2:10 AM, joanna wrote:
> dpb <none@non.net> wrote in message <lppu18$1i5$1@speranza.aioe.org>...
>> On 07/11/2014 4:19 PM, dpb wrote:
>> ...
>>
>> > dn=filename(:,1); % a shorter, more mnemonic name for the datenum
>> > hAx=plotyy(x,a1,x,a2,@plot,@stem);
>>
>> ERRATUM: Switched later; missed changing 'x' to 'dn'...
>> hAx=plotyy(dn,a1,dn,a2,@plot,@stem);
>>
>> > set(hAx(2),'xtick',[]) % hide the RH x-axis labels so don't see'um
>> > datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')
>>
>> ...
>>
...

> The 1st column isn't a Matlab datenum,it's simple data serial numbers.
> The 3rd column is days,4th is months and 5th are years.
> I tried what you suggested but i got this message:
...
> Error in ==> vetaplot1 at 30
> datetick(hAx(1),'x','dd-mm-yyyy','keeptick','keeplimits')

Typo -- 'keeptick' --> 'keepticks'

But datetick() will _NOT_ work correctly unless you convert the serial
dates to Matlab datenums per the documentation.

dn=datenum(fliplr(x(:,3:5))); % convert to datenums

Now use datetick() after plot against dn

Or, you could set 'xticklabel' directly by formatting the date columns

hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
set(hAx,'xticklabel',[])
set(hAx(1),'xtick',[x(:,1)])
set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))

You may have to reduce the number of ticks; these labels will take up a
lot of real estate.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 11:49:45

Message: 6 of 27

On 07/12/2014 6:20 AM, dpb wrote:
...

> Or, you could set 'xticklabel' directly by formatting the date columns
>
> hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
> set(hAx,'xticklabel',[])
> set(hAx(1),'xtick',[x(:,1)])
> set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
...

  set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))

For your case the handle should be hAx(1); I just verified the
formatting on a single plot() call here so used gca instead...

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 12 Jul, 2014 15:27:13

Message: 7 of 27

dpb <none@non.net> wrote in message <lpr7bm$jbi$1@speranza.aioe.org>...
> On 07/12/2014 6:20 AM, dpb wrote:
> ...
>
> > Or, you could set 'xticklabel' directly by formatting the date columns
> >
> > hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
> > set(hAx,'xticklabel',[])
> > set(hAx(1),'xtick',[x(:,1)])
> > set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
> ...
>
> set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
>
> For your case the handle should be hAx(1); I just verified the
> formatting on a single plot() call here so used gca instead...
>
> --
>
Converted dn to datenum.
I used datetick () afterwards.
Got both forms of data on x axis (dates and serial numbers) and dates have the form 01-Jan-0000,01-Jan-0020,01-Jan-0040 and so on...
Plus i got an error:
??? Invalid handle object.

Error in ==> vetaplot1 at 45
set(a2,'color','r')

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 16:46:28

Message: 8 of 27

On 07/12/2014 10:27 AM, joanna wrote:
> dpb <none@non.net> wrote in message <lpr7bm$jbi$1@speranza.aioe.org>...
>> On 07/12/2014 6:20 AM, dpb wrote:
>> ...
>>
>> > Or, you could set 'xticklabel' directly by formatting the date columns
>> >
>> > hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
>> > set(hAx,'xticklabel',[])
>> > set(hAx(1),'xtick',[x(:,1)])
>> > set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
>> ...
>>
>> set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
>>
>> For your case the handle should be hAx(1); I just verified the
>> formatting on a single plot() call here so used gca instead...
>>
>> --
>>
> Converted dn to datenum.
> I used datetick () afterwards.
> Got both forms of data on x axis (dates and serial numbers) and dates
> have the form 01-Jan-0000,01-Jan-0020,01-Jan-0040 and so on...
> Plus i got an error:
> ??? Invalid handle object.
>
> Error in ==> vetaplot1 at 45
> set(a2,'color','r')

You didn't use the handle you saved, then. As I wrote it, the two axes
handles are in array hAx() and the

set(hAx,'xticklabel',[])

will turn the labels off on both prior to setting hAx(1) with datetick()

I see since I only used a single axes for testing the formatting I cut
'n pasted and missed eliminating the (1) subscript so if you didn't
fixup the intent but just copied the datenum values will still be
showing on the RH axes.

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 12 Jul, 2014 16:58:09

Message: 9 of 27

dpb <none@non.net> wrote in message <lprop6$66p$1@dont-email.me>...
> On 07/12/2014 10:27 AM, joanna wrote:
> > dpb <none@non.net> wrote in message <lpr7bm$jbi$1@speranza.aioe.org>...
> >> On 07/12/2014 6:20 AM, dpb wrote:
> >> ...
> >>
> >> > Or, you could set 'xticklabel' directly by formatting the date columns
> >> >
> >> > hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
> >> > set(hAx,'xticklabel',[])
> >> > set(hAx(1),'xtick',[x(:,1)])
> >> > set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
> >> ...
> >>
> >> set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
> >>
> >> For your case the handle should be hAx(1); I just verified the
> >> formatting on a single plot() call here so used gca instead...
> >>
> >> --
> >>
> > Converted dn to datenum.
> > I used datetick () afterwards.
> > Got both forms of data on x axis (dates and serial numbers) and dates
> > have the form 01-Jan-0000,01-Jan-0020,01-Jan-0040 and so on...
> > Plus i got an error:
> > ??? Invalid handle object.
> >
> > Error in ==> vetaplot1 at 45
> > set(a2,'color','r')
>
> You didn't use the handle you saved, then. As I wrote it, the two axes
> handles are in array hAx() and the
>
> set(hAx,'xticklabel',[])
>
> will turn the labels off on both prior to setting hAx(1) with datetick()
>
> I see since I only used a single axes for testing the formatting I cut
> 'n pasted and missed eliminating the (1) subscript so if you didn't
> fixup the intent but just copied the datenum values will still be
> showing on the RH axes.
>
> --
You're right!Fixed it!
But what about the dates?
01-Jan-0000,01-Jan-0020 and so on aren't the correct dates that i have in my file and i want to be displayed in x axis....

Subject: put date in x axis when using plotyy

From: joanna

Date: 12 Jul, 2014 17:02:08

Message: 10 of 27

"joanna" wrote in message <lprpf1$1mh$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <lprop6$66p$1@dont-email.me>...
> > On 07/12/2014 10:27 AM, joanna wrote:
> > > dpb <none@non.net> wrote in message <lpr7bm$jbi$1@speranza.aioe.org>...
> > >> On 07/12/2014 6:20 AM, dpb wrote:
> > >> ...
> > >>
> > >> > Or, you could set 'xticklabel' directly by formatting the date columns
> > >> >
> > >> > hAx=plotyy(x(:,1),a1,x(:,1),a2,@plot,@stem);
> > >> > set(hAx,'xticklabel',[])
> > >> > set(hAx(1),'xtick',[x(:,1)])
> > >> > set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
> > >> ...
> > >>
> > >> set(gca,'xticklabel',num2str(x,'%02d-%02d-%4d'))
> > >>
> > >> For your case the handle should be hAx(1); I just verified the
> > >> formatting on a single plot() call here so used gca instead...
> > >>
> > >> --
> > >>
> > > Converted dn to datenum.
> > > I used datetick () afterwards.
> > > Got both forms of data on x axis (dates and serial numbers) and dates
> > > have the form 01-Jan-0000,01-Jan-0020,01-Jan-0040 and so on...
> > > Plus i got an error:
> > > ??? Invalid handle object.
> > >
> > > Error in ==> vetaplot1 at 45
> > > set(a2,'color','r')
> >
> > You didn't use the handle you saved, then. As I wrote it, the two axes
> > handles are in array hAx() and the
> >
> > set(hAx,'xticklabel',[])
> >
> > will turn the labels off on both prior to setting hAx(1) with datetick()
> >
> > I see since I only used a single axes for testing the formatting I cut
> > 'n pasted and missed eliminating the (1) subscript so if you didn't
> > fixup the intent but just copied the datenum values will still be
> > showing on the RH axes.
> >
> > --
> You're right!Fixed it!
> But what about the dates?
> 01-Jan-0000,01-Jan-0020 and so on aren't the correct dates that i have in my file and i want to be displayed in x axis....
And i'm still getting the previous error message..

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 17:21:42

Message: 11 of 27

On 07/12/2014 12:02 PM, joanna wrote:
...

>> But what about the dates?
>> 01-Jan-0000,01-Jan-0020 and so on aren't the correct dates that i have
>> in my file and i want to be displayed in x axis....
> And i'm still getting the previous error message..

Well, since I don't actually have any of your data or the actual code,
it's tough to tell where you went wrong. It works here with the data as
you described for a set of dates.

If you want further debugging assistance, paste a short section of the
data file and the code you used and any error messages in toto and in
context...

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 12 Jul, 2014 18:33:10

Message: 12 of 27

On 07/12/2014 12:02 PM, joanna wrote:
...

>> But what about the dates?
>> 01-Jan-0000,01-Jan-0020 and so on aren't the correct dates that i have
>> in my file and i want to be displayed in x axis....
> And i'm still getting the previous error message..

OK, here's a sample dataset that afaict meets your spec for the dates.
I just used a random value to plot in the 2nd column...

 >> num2str(x,'%5d %.3f %5d %5d %5d') % 'x' array pretty-printed...
ans =
0 0.164 1 1 2010
1 0.280 2 1 2010
2 0.259 3 1 2010
3 0.547 4 1 2010
4 0.541 5 1 2010

 >> dn=datenum(fliplr(x(:,3:5))); % datenum expects y,m,d
 >> hAx=plotyy(dn,x(:,2),dn,x(:,2)*10,@plot,@stem); % vs datenums
 >> set(hAx,'xticklabel',[]) % wipe tick labels
 >> set(hAx,'xtick',dn) % make # ticks match # dates
 >> datetick(hAx(1),'x','yyyy-mm-dd','keepticks','keeplimits')

The above gives the dates as in the x array and no errors/warnings.

If your data aren't as I've interpreted your description, salt to suit
actual arrangement.

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 13 Jul, 2014 08:28:08

Message: 13 of 27

dpb <none@non.net> wrote in message <lprv17$hkd$1@dont-email.me>...
> On 07/12/2014 12:02 PM, joanna wrote:
> ...
>
> >> But what about the dates?
> >> 01-Jan-0000,01-Jan-0020 and so on aren't the correct dates that i have
> >> in my file and i want to be displayed in x axis....
> > And i'm still getting the previous error message..
>
> OK, here's a sample dataset that afaict meets your spec for the dates.
> I just used a random value to plot in the 2nd column...
>
> >> num2str(x,'%5d %.3f %5d %5d %5d') % 'x' array pretty-printed...
> ans =
> 0 0.164 1 1 2010
> 1 0.280 2 1 2010
> 2 0.259 3 1 2010
> 3 0.547 4 1 2010
> 4 0.541 5 1 2010
>
> >> dn=datenum(fliplr(x(:,3:5))); % datenum expects y,m,d
> >> hAx=plotyy(dn,x(:,2),dn,x(:,2)*10,@plot,@stem); % vs datenums
> >> set(hAx,'xticklabel',[]) % wipe tick labels
> >> set(hAx,'xtick',dn) % make # ticks match # dates
> >> datetick(hAx(1),'x','yyyy-mm-dd','keepticks','keeplimits')
>
> The above gives the dates as in the x array and no errors/warnings.
>
> If your data aren't as I've interpreted your description, salt to suit
> actual arrangement.

> My data are as you posted except my dates aren't consecutive (i.e i have days that are missing or i have sets of values refering to the same date)..
I guess that's why i get new error:
??? Error using ==> set
Values must be monotonically increasing.

Error in ==> vetaplot1 at 30
set(yaxes(1),'xtick',dn)

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 11:36:30

Message: 14 of 27

On 07/13/2014 3:28 AM, joanna wrote:
...

>> My data are as you posted except my dates aren't consecutive (i.e i
>> have days that are missing or i have sets of values refering to the
>> same date)..
> I guess that's why i get new error:
> ??? Error using ==> set
> Values must be monotonically increasing.
>
> Error in ==> vetaplot1 at 30
> set(yaxes(1),'xtick',dn)

Then use a sequence of datenum values from some start to end time for
the axis tick values that covers the range and that are reasonably
spaced for the range of dates you have in the data set.

datenum() is ideal for creating such evenly spaced values by a day
interval or whatever time unit is appropriate as a base year,month,day
with a column vector of ranges for whichever is the desired variate as
it will intelligently wrap around days for example accounting for the
proper number per month.

Then you just plot the data onto the axes as any other set of x,y data.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 12:09:52

Message: 15 of 27

On 07/13/2014 6:36 AM, dpb wrote:
...

> Then use a sequence of datenum values from some start to end time for
> the axis tick values that covers the range and that are reasonably
> spaced for the range of dates you have in the data set.
>
...

Or, to just get something to work on, just plot versus the datenum
values letting Matlab set the axis limits as it chooses and see how it
looks, cleaning up only if don't get a pleasing result.

You can try datetick() without the 'keeplimits' and/or 'keepticks'
optional arguments and see what the default spacing turns out to be--it
might just be ok as is.

The point is that manipulating the axis settings is essentially no
different than set tick intervals for any other x-axis other than it is
in units of datenums and there's the one other step of using datetick to
format the date labels for viewing.

You just have to remember that that with plotyy() there are two x-axes
so when/if you change 'xlimit' or 'xtick' property on one you must do
the same thing on the other so they remain consistent. That's why I
turned off the 'xticklabel' initially so it doesn't display until the
other machinations are done.

You can make your case simpler by only using plot() and save having to
fiddle with the second axis while you're working out the details...saves
a fair amount of effort of not having to do the second axis until you
know what it is that actually want.

An enhancement I've always wished for would be for the RH x-axes
properties to be slaved to the LH automagically so that one only had to
deal with one -- or, in essence, a way to set it such that there
appeared to be only one x-axis object.

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 13 Jul, 2014 15:23:11

Message: 16 of 27

dpb <none@non.net> wrote in message <lptsug$heg$1@dont-email.me>...
> On 07/13/2014 6:36 AM, dpb wrote:
> ...
>
> > Then use a sequence of datenum values from some start to end time for
> > the axis tick values that covers the range and that are reasonably
> > spaced for the range of dates you have in the data set.
> >
> ...
>
> Or, to just get something to work on, just plot versus the datenum
> values letting Matlab set the axis limits as it chooses and see how it
> looks, cleaning up only if don't get a pleasing result.
>
> You can try datetick() without the 'keeplimits' and/or 'keepticks'
> optional arguments and see what the default spacing turns out to be--it
> might just be ok as is.
>
> The point is that manipulating the axis settings is essentially no
> different than set tick intervals for any other x-axis other than it is
> in units of datenums and there's the one other step of using datetick to
> format the date labels for viewing.
>
> You just have to remember that that with plotyy() there are two x-axes
> so when/if you change 'xlimit' or 'xtick' property on one you must do
> the same thing on the other so they remain consistent. That's why I
> turned off the 'xticklabel' initially so it doesn't display until the
> other machinations are done.
>
> You can make your case simpler by only using plot() and save having to
> fiddle with the second axis while you're working out the details...saves
> a fair amount of effort of not having to do the second axis until you
> know what it is that actually want.
>
> An enhancement I've always wished for would be for the RH x-axes
> properties to be slaved to the LH automagically so that one only had to
> deal with one -- or, in essence, a way to set it such that there
> appeared to be only one x-axis object.
>
> --
Thank you for all the help,answers and suggestions!
I've been trying for several hours but nothing seems to work the way i want to..
I'm using datetick() without 'keeplimits' and 'keepticks' because otherwise it doesn't work with my data..
I'm still getting dates in the form 01-Jan-0000,01-Jan-0020 and so on and an errror conserning properties of yaxes(2)...
Anyway,thanks again for all!

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 15:43:37

Message: 17 of 27

On 07/13/2014 10:23 AM, joanna wrote:
...

> Thank you for all the help,answers and suggestions!
> I've been trying for several hours but nothing seems to work the way i
> want to..
> I'm using datetick() without 'keeplimits' and 'keepticks' because
> otherwise it doesn't work with my data..
> I'm still getting dates in the form 01-Jan-0000,01-Jan-0020 and so on
> and an errror conserning properties of yaxes(2)...
> Anyway,thanks again for all!

Well, it's _got_ to be relatively simple problem to fix...there's
something inconsistent in what you're inputting to the datenum()
function to be getting the years screwed that way--probably you've got
the wrong column order or somesuch.

How big is the data file or, better yet, can you recreate the problem
with just a small subset of it, say 10 lines or so, so that you can just
paste that section and your code that reproduces an example of the problem?

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 13 Jul, 2014 16:11:11

Message: 18 of 27

dpb <none@non.net> wrote in message <lpu9f9$486$1@dont-email.me>...
> On 07/13/2014 10:23 AM, joanna wrote:
> ...
>
> > Thank you for all the help,answers and suggestions!
> > I've been trying for several hours but nothing seems to work the way i
> > want to..
> > I'm using datetick() without 'keeplimits' and 'keepticks' because
> > otherwise it doesn't work with my data..
> > I'm still getting dates in the form 01-Jan-0000,01-Jan-0020 and so on
> > and an errror conserning properties of yaxes(2)...
> > Anyway,thanks again for all!
>
> Well, it's _got_ to be relatively simple problem to fix...there's
> something inconsistent in what you're inputting to the datenum()
> function to be getting the years screwed that way--probably you've got
> the wrong column order or somesuch.
>
> How big is the data file or, better yet, can you recreate the problem
> with just a small subset of it, say 10 lines or so, so that you can just
> paste that section and your code that reproduces an example of the problem?
>
> --My code:

%trial.m
load data.dat;
%definition of mimimum veta value
minveta=min(data(:,2))
%definition of row (r) and column (c)index of mimimum veta value
[r,c]=find(data(:,2)==minveta)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
veta=data(:,2);
ML=data(:,9);
dn=datenum(data(:,1));
[yaxes,veta,ML]=plotyy(dn,veta,dn,ML,'plot','stem');
%set axes labels and properties
set(yaxes,'xticklabel',[])
set(yaxes(1),'xtick',dn)
datetick('x',1)
set(yaxes(2),'ylim',[5.5 7])
set(yaxes(2),'ytick',[5.5:0.5:7])
set(yaxes(1),'box','off')
axes(yaxes(1));
ylabel('?')
axes(yaxes(2));
ylabel('ML')
title('dataplot');
set(ML,'color','r')
set(ML,'marker','*')
% end of trial.m


and a sample of my data:
1.0000000e+000 6.9247600e-001 2.0000000e+000 6.0000000e+000 1.9660000e+003 2.2000000e+001 5.1000000e+001 2.3000000e+001 4.4000000e+000 3.9810717e+006
  2.0000000e+000 6.8464800e-001 3.0000000e+000 6.0000000e+000 1.9660000e+003 1.0000000e+000 3.1000000e+001 5.2600000e+001 3.3000000e+000 8.9125094e+004
  3.0000000e+000 6.7682400e-001 4.0000000e+000 6.0000000e+000 1.9660000e+003 1.0000000e+000 5.0000000e+000 1.3000000e+001 3.2000000e+000 6.3095734e+004
  4.0000000e+000 6.6890300e-001 4.0000000e+000 6.0000000e+000 1.9660000e+003 2.0000000e+000 2.1000000e+001 1.8000000e+000 4.0000000e+000 1.0000000e+006
  5.0000000e+000 6.6102700e-001 5.0000000e+000 6.0000000e+000 1.9660000e+003 2.0000000e+001 5.1000000e+001 5.7300000e+001 3.9000000e+000 7.0794578e+005
  6.0000000e+000 6.5310600e-001 6.0000000e+000 6.0000000e+000 1.9660000e+003 1.9000000e+001 3.8000000e+001 4.0600000e+001 3.6000000e+000 2.5118864e+005
  7.0000000e+000 6.4528500e-001 8.0000000e+000 6.0000000e+000 1.9660000e+003 8.0000000e+000 2.0000000e+000 1.1300000e+001 3.5000000e+000 1.7782794e+005
  8.0000000e+000 6.3754700e-001 8.0000000e+000 6.0000000e+000 1.9660000e+003 8.0000000e+000 3.9000000e+001 0.0000000e+000 3.6000000e+000 2.5118864e+005
  9.0000000e+000 6.2996300e-001 8.0000000e+000 6.0000000e+000 1.9660000e+003 2.0000000e+001 1.5000000e+001 4.0000000e-001 3.4000000e+000 1.2589254e+005
  1.0000000e+001 6.2255000e-001 1.1000000e+001 6.0000000e+000 1.9660000e+003 1.0000000e+001 2.1000000e+001 5.2000000e+001 4.2000000e+000 1.9952623e+006

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 16:34:45

Message: 19 of 27

On 07/13/2014 11:11 AM, joanna wrote:
> %trial.m
> load data.dat;
> %definition of mimimum veta value
> minveta=min(data(:,2))
> %definition of row (r) and column (c)index of mimimum veta value
> [r,c]=find(data(:,2)==minveta)
> veta=data(:,2);
> ML=data(:,9);
> dn=datenum(data(:,1));

There's the problem!!!

...

> and a sample of my data:
> 1.0000000e+000 6.9247600e-001 2.0000000e+000 6.0000000e+000
> 1.9660000e+003 2.2000000e+001 5.1000000e+001 2.3000000e+001
> 4.4000000e+000 3.9810717e+006
> 2.0000000e+000 6.8464800e-001 3.0000000e+000 6.0000000e+000
> 1.9660000e+003 1.0000000e+000 3.1000000e+001 5.2600000e+001
> 3.3000000e+000 8.9125094e+004
> 3.0000000e+000 6.7682400e-001 4.0000000e+000 6.0000000e+000
> 1.9660000e+003 1.0000000e+000 5.0000000e+000 1.3000000e+001
> 3.2000000e+000 6.3095734e+004
...

You've completely ignored my instructions. :(

Use the m,d,y columns to compute the datenums as I illustrated, _NOT_
the serial numbers. They're really of no consequence for plotting
versus calendar time.

As before,

  dn=datenum(fliplr(data(:,3:5));

and all will be well.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 16:47:52

Message: 20 of 27

On 07/13/2014 11:34 AM, dpb wrote:
...

> Use the m,d,y columns to compute the datenums as I illustrated, _NOT_
> the serial numbers. They're really of no consequence for plotting versus
> calendar time.
>
> As before,
>
> dn=datenum(fliplr(data(:,3:5));

...

Add missing closing parens...

dn=datenum(fliplr(data(:,3:5)));


--

Subject: put date in x axis when using plotyy

From: joanna

Date: 13 Jul, 2014 16:52:13

Message: 21 of 27

dpb <none@non.net> wrote in message <lpucf8$p9v$1@dont-email.me>...
> On 07/13/2014 11:11 AM, joanna wrote:
> > %trial.m
> > load data.dat;
> > %definition of mimimum veta value
> > minveta=min(data(:,2))
> > %definition of row (r) and column (c)index of mimimum veta value
> > [r,c]=find(data(:,2)==minveta)
> > veta=data(:,2);
> > ML=data(:,9);
> > dn=datenum(data(:,1));
>
> There's the problem!!!
>
> ...
>
> > and a sample of my data:
> > 1.0000000e+000 6.9247600e-001 2.0000000e+000 6.0000000e+000
> > 1.9660000e+003 2.2000000e+001 5.1000000e+001 2.3000000e+001
> > 4.4000000e+000 3.9810717e+006
> > 2.0000000e+000 6.8464800e-001 3.0000000e+000 6.0000000e+000
> > 1.9660000e+003 1.0000000e+000 3.1000000e+001 5.2600000e+001
> > 3.3000000e+000 8.9125094e+004
> > 3.0000000e+000 6.7682400e-001 4.0000000e+000 6.0000000e+000
> > 1.9660000e+003 1.0000000e+000 5.0000000e+000 1.3000000e+001
> > 3.2000000e+000 6.3095734e+004
> ...
>
> You've completely ignored my instructions. :(
>
> Use the m,d,y columns to compute the datenums as I illustrated, _NOT_
> the serial numbers. They're really of no consequence for plotting
> versus calendar time.
>
> As before,
>
> dn=datenum(fliplr(data(:,3:5));
>
> and all will be well.
>
> --
When i use:
%trial.m
load data.dat;
%definition of mimimum veta value
%!!!!!attention to the filename!!!!
minveta=min(data(:,2))
%definition of row (r) and column (c)index of mimimum veta value
[r,c]=find(data(:,2)==minveta)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
veta=data(:,2);
ML=data(:,9);
dn=datenum(data(:,3:5));
[yaxes,veta,ML]=plotyy(dn,veta,dn,ML,'plot','stem');
%set axes labels and properties
set(yaxes,'xticklabel',[])
set(yaxes(1),'xtick',dn)
datetick('x',1)
set(yaxes(2),'ylim',[5.5 7])
set(yaxes(2),'ytick',[5.5:0.5:7])
set(yaxes(1),'box','off')
axes(yaxes(1));
ylabel('?')
axes(yaxes(2));
ylabel('ML')
title('dataplot');
set(ML,'color','r')
set(ML,'marker','*')
% end of trial.m


i.e dn=datenum(data(:,3:5));
i get a messed up diagram and and an error:

??? Error using ==> set
Values must be monotonically increasing.

Error in ==> trial at 15
set(yaxes(1),'xtick',dn)

I don't know what else to do...

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 17:03:22

Message: 22 of 27

On 07/13/2014 11:52 AM, joanna wrote:
> dpb <none@non.net> wrote in message <lpucf8$p9v$1@dont-email.me>...
>> On 07/13/2014 11:11 AM, joanna wrote:
...

>> > and a sample of my data:
>> > 1.0000000e+000 6.9247600e-001 2.0000000e+000 6.0000000e+000
>> > 1.9660000e+003 2.2000000e+001 5.1000000e+001 2.3000000e+001
>> > 4.4000000e+000 3.9810717e+006
>> > 2.0000000e+000 6.8464800e-001 3.0000000e+000 6.0000000e+000
>> > 1.9660000e+003 1.0000000e+000 3.1000000e+001 5.2600000e+001
>> > 3.3000000e+000 8.9125094e+004
>> > 3.0000000e+000 6.7682400e-001 4.0000000e+000 6.0000000e+000
>> > 1.9660000e+003 1.0000000e+000 5.0000000e+000 1.3000000e+001
>> > 3.2000000e+000 6.3095734e+004
>> ...
>>
...

>> dn=datenum(fliplr(data(:,3:5));
>>
...

> dn=datenum(data(:,3:5));

Yet again, you didn't fix up the order of the data into datenum--it's
significant. What's so hard about following the roadmap I left
you?????? You passed it d,m,y according to your written description but
needs must pass y,m,d in that order.

dn=datenum(fliplr(data(:,3:5))); % datenum expects (REQUIRES) y,m,d

> I don't know what else to do...

Following the rules would help. READ and FOLLOW the documentation for
the functions you're trying to use. Use the help I've given; make
changes only if there's a good and valid reason to do so.

--

Subject: put date in x axis when using plotyy

From: joanna

Date: 13 Jul, 2014 17:19:16

Message: 23 of 27

dpb <none@non.net> wrote in message <lpue4q$5gf$1@dont-email.me>...
> On 07/13/2014 11:52 AM, joanna wrote:
> > dpb <none@non.net> wrote in message <lpucf8$p9v$1@dont-email.me>...
> >> On 07/13/2014 11:11 AM, joanna wrote:
> ...
>
> >> > and a sample of my data:
> >> > 1.0000000e+000 6.9247600e-001 2.0000000e+000 6.0000000e+000
> >> > 1.9660000e+003 2.2000000e+001 5.1000000e+001 2.3000000e+001
> >> > 4.4000000e+000 3.9810717e+006
> >> > 2.0000000e+000 6.8464800e-001 3.0000000e+000 6.0000000e+000
> >> > 1.9660000e+003 1.0000000e+000 3.1000000e+001 5.2600000e+001
> >> > 3.3000000e+000 8.9125094e+004
> >> > 3.0000000e+000 6.7682400e-001 4.0000000e+000 6.0000000e+000
> >> > 1.9660000e+003 1.0000000e+000 5.0000000e+000 1.3000000e+001
> >> > 3.2000000e+000 6.3095734e+004
> >> ...
> >>
> ...
>
> >> dn=datenum(fliplr(data(:,3:5));
> >>
> ...
>
> > dn=datenum(data(:,3:5));
>
> Yet again, you didn't fix up the order of the data into datenum--it's
> significant. What's so hard about following the roadmap I left
> you?????? You passed it d,m,y according to your written description but
> needs must pass y,m,d in that order.
>
> dn=datenum(fliplr(data(:,3:5))); % datenum expects (REQUIRES) y,m,d
>
> > I don't know what else to do...
>
> Following the rules would help. READ and FOLLOW the documentation for
> the functions you're trying to use. Use the help I've given; make
> changes only if there's a good and valid reason to do so.
>
> --It doesn't work with fliplr either..
I get the same error:
??? Error using ==> set
Values must be monotonically increasing.

Error in ==> trial at 15
set(yaxes(1),'xtick',dn)

Anyway,thanks again for all..

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 17:26:07

Message: 24 of 27

On 07/13/2014 11:52 AM, joanna wrote:
...

> i.e dn=datenum(data(:,3:5));
> i get a messed up diagram and and an error:
...

As example of _not_ paying attention to detail...

I put a few lines of your data into a file and

 >> data=textread('joanna.dat');

First, what you did (against my express code gave you at least three or
four times)...

 >> datestr(datenum(data(3:5)))
ans =
31-Dec-0002 16:25:53
 >> datestr(datenum(data(:,3:5)))
ans =
18-Oct-0007
17-Oct-0008
18-Oct-0009

If use the correct order of inputs instead...

 >> datestr(datenum(fliplr(data(:,3:5))))
ans =
02-Jun-1966
03-Jun-1966
04-Jun-1966
 >>

Again, if there are data points in the overall array for multiple
samples on a given date, just plot versus datenum and they'll be in the
correct place. Then just set the tick values as needed to get a usable
display.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 17:29:25

Message: 25 of 27

On 07/13/2014 12:19 PM, joanna wrote:
...

>> --It doesn't work with fliplr either..
> I get the same error:
> ??? Error using ==> set
> Values must be monotonically increasing.
>
> Error in ==> trial at 15
> set(yaxes(1),'xtick',dn)

As I also said, _IF_ there are duplicate values then do _NOT_ try to set
'xtick'--just use the default that Matlab will do on its own.

You may then want/need to fixup the number of ticks and/or limits for
"prettying up" the result.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 13 Jul, 2014 18:42:44

Message: 26 of 27

On 07/13/2014 12:19 PM, joanna wrote:
...

>> --It doesn't work with fliplr either..
> I get the same error:
> ??? Error using ==> set
> Values must be monotonically increasing.
>
> Error in ==> trial at 15
> set(yaxes(1),'xtick',dn)

OK, I overlooked that there were some repeated dates in your pasted
example--was hard to see w/ the line wrap...

OK, this works here as advertised, albeit as suspected owing to the
length of the labels its pretty crowded.

 >> dn=datenum(fliplr(data(:,3:5)));
 >> datestr(dn)
ans =
02-Jun-1966
03-Jun-1966
04-Jun-1966
04-Jun-1966
05-Jun-1966
06-Jun-1966
08-Jun-1966
08-Jun-1966
08-Jun-1966
11-Jun-1966
 >> hAx=plotyy(dn,data(:,2),dn,data(:,9),@plot,@stem);
 >> set(hAx,'xticklab',[])
 >> datetick(hAx(1),'x','yyyy-mm-dd','keepticks')

That's run together until

 >> set(hAx(1),'fontsize',8)

and stretch the size of the x-axis some.

 >> datetick(hAx(1),'x','yy-mm-dd','keepticks')

This is somewhat more legible.

You'll have to experiment with how many and where it places the tick
marks with your full data set. As noted, you may want to do something
with generating only a half-dozen or so.

There are many "variations on a theme" available here--one of the
simplest is something on the order of--

 >> dt=linspace(dn(1),dn(end),6);
 >> set(hAx,'xtick',dt)
 >>

Alternatively, you can also set the xlim values to datenums
corresponding to the beginning of a month and/or year and as suggested
previously use the vectorizing capability to increment by 6 months or
some other suitable interval so that the ticks line up with specific
"neat" calendar intervals.

Again, the key is to decide what you do want and then work to achieve
that result. The tools/methods to manipulate the dates and display them
are all demonstrated here.

This again presumes that you plot the data against the actual date
values. If it is mandatory to use the sequential serial number instead
so that the multiple data values show up as separate locations on the
x-axis, then you've got to generate a different problem as you'll be
asking to plot the location at something different than the actual date.
  There are some ways I can think of to handle that, but they would take
additional fiddling to make something useful of the x-axis.
My first inclination would be to separate the repeated values into
alternate datasets and plot them as other data points color-coded
perhaps as the second, third, etc., repeat for the day but still use the
actual date on the axis.

Otherwise, you'll be forced to generate some labels at points from the
actual date and set them appropriately but the indication to the user
will always be somewhat misleading; the more so the more repeats there
are so the accumulated distance of zero actual time versus displayed
axis time becomes larger.

--

Subject: put date in x axis when using plotyy

From: dpb

Date: 14 Jul, 2014 13:50:28

Message: 27 of 27

On 07/13/2014 12:29 PM, dpb wrote:
...

> As I also said, _IF_ there are duplicate values then do _NOT_ try to set
> 'xtick'--just use the default that Matlab will do on its own.
...

Also, in general you don't want to set 'xtick' to the values in the
x-axis vector anyway--that normally will put ticks on the axis like hair
on a dog's back--_way_ too many to be useful. I only did it in the
example where I had just a few points to make the two coincide.

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us