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:
Subplots with axis as percentages

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 27 May, 2014 09:16:07

Message: 1 of 13

Hi,

I am trying to change values on X-axis scatter plots to %, and I am using the following code:

h=gca;
labels=get(h,'xticklabel'); % get the x axis labels
labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
set(h,'xticklabel',labels_modif);

This works perfectly when I work with scatters in isolation, but when I run my scatters as part of a subplot, things break down. I get the following error:

??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> Figure at 80
labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];

What is going wrong here?

Regards Ulrik.

Subject: Subplots with axis as percentages

From: dpb

Date: 27 May, 2014 12:31:25

Message: 2 of 13

On 5/27/2014 4:16 AM, Ulrik Nash wrote:
> labels=get(h,'xticklabel'); % get the x axis labels
> labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];

I see a couple places this could go wrong...but the most likely is

ones(length(labels),1)

for a subplot there might be fewer labels than the length of one.

But, I'd rewrite the whole thing to be a little less verbose and
eliminate the string>number>string to just number>string...

  set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))

You also do need to ensure h is just a single axes handle here, not an
array of handles. If that were the case, then need to encapsulate in
cell arrays.

--

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 27 May, 2014 13:14:09

Message: 3 of 13

dpb <none@non.net> wrote in message <lm20j1$1de$1@speranza.aioe.org>...
> On 5/27/2014 4:16 AM, Ulrik Nash wrote:
> > labels=get(h,'xticklabel'); % get the x axis labels
> > labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
>
> I see a couple places this could go wrong...but the most likely is
>
> ones(length(labels),1)
>
> for a subplot there might be fewer labels than the length of one.
>
> But, I'd rewrite the whole thing to be a little less verbose and
> eliminate the string>number>string to just number>string...
>
> set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))
>
> You also do need to ensure h is just a single axes handle here, not an
> array of handles. If that were the case, then need to encapsulate in
> cell arrays.


That removed the error, but created another problem, which I think might be related to what you stated last.

My axes numbers seem to loop. 2% 4% 6% 8% 2% 4% and so on...

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 27 May, 2014 13:15:09

Message: 4 of 13

dpb <none@non.net> wrote in message <lm20j1$1de$1@speranza.aioe.org>...
> On 5/27/2014 4:16 AM, Ulrik Nash wrote:
> > labels=get(h,'xticklabel'); % get the x axis labels
> > labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
>
> I see a couple places this could go wrong...but the most likely is
>
> ones(length(labels),1)
>
> for a subplot there might be fewer labels than the length of one.
>
> But, I'd rewrite the whole thing to be a little less verbose and
> eliminate the string>number>string to just number>string...
>
> set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))
>
> You also do need to ensure h is just a single axes handle here, not an
> array of handles. If that were the case, then need to encapsulate in
> cell arrays.




.... But individually there is no looping. Only when run as subplot....
> --
>
>

Subject: Subplots with axis as percentages

From: Steven Lord

Date: 27 May, 2014 13:54:48

Message: 5 of 13


"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
news:lm2331$egc$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <lm20j1$1de$1@speranza.aioe.org>...
>> On 5/27/2014 4:16 AM, Ulrik Nash wrote:
>> > labels=get(h,'xticklabel'); % get the x axis labels
>> > labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
>>
>> I see a couple places this could go wrong...but the most likely is
>>
>> ones(length(labels),1)
>>
>> for a subplot there might be fewer labels than the length of one.
>>
>> But, I'd rewrite the whole thing to be a little less verbose and
>> eliminate the string>number>string to just number>string...
>>
>> set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))
>>
>> You also do need to ensure h is just a single axes handle here, not an
>> array of handles. If that were the case, then need to encapsulate in
>> cell arrays.
>
>
> That removed the error, but created another problem, which I think might
> be related to what you stated last.
>
> My axes numbers seem to loop. 2% 4% 6% 8% 2% 4% and so on...

My guess is that some of your subplots have fewer ticks than other subplots
and you're somehow using the number of ticks for one to generate tick labels
for another. But that's just a guess; post a small but self-contained and
executable section of code with which you can reproduce the problem if you
want the group to confirm or refute that guess.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 27 May, 2014 14:10:15

Message: 6 of 13

"Steven Lord" <Steven_Lord@mathworks.com> wrote in message <lm25fc$lq4$1@newscl01ah.mathworks.com>...
>
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
> news:lm2331$egc$1@newscl01ah.mathworks.com...
> > dpb <none@non.net> wrote in message <lm20j1$1de$1@speranza.aioe.org>...
> >> On 5/27/2014 4:16 AM, Ulrik Nash wrote:
> >> > labels=get(h,'xticklabel'); % get the x axis labels
> >> > labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
> >>
> >> I see a couple places this could go wrong...but the most likely is
> >>
> >> ones(length(labels),1)
> >>
> >> for a subplot there might be fewer labels than the length of one.
> >>
> >> But, I'd rewrite the whole thing to be a little less verbose and
> >> eliminate the string>number>string to just number>string...
> >>
> >> set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))
> >>
> >> You also do need to ensure h is just a single axes handle here, not an
> >> array of handles. If that were the case, then need to encapsulate in
> >> cell arrays.
> >
> >
> > That removed the error, but created another problem, which I think might
> > be related to what you stated last.
> >
> > My axes numbers seem to loop. 2% 4% 6% 8% 2% 4% and so on...
>
> My guess is that some of your subplots have fewer ticks than other subplots
> and you're somehow using the number of ticks for one to generate tick labels
> for another. But that's just a guess; post a small but self-contained and
> executable section of code with which you can reproduce the problem if you
> want the group to confirm or refute that guess.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Hi Steve,

I suspect you are right. Unfortunately your request is quite cumbersome, since I am importing data and the subplot is 4x2.

If we assume you are right, can you send me in the right direction? I can then look at it myself?

Subject: Subplots with axis as percentages

From: dpb

Date: 27 May, 2014 17:56:28

Message: 7 of 13

On 5/27/2014 9:10 AM, Ulrik Nash wrote:
> "Steven Lord" <Steven_Lord@mathworks.com> wrote in message
> <lm25fc$lq4$1@newscl01ah.mathworks.com>...
>> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message
>> news:lm2331$egc$1@newscl01ah.mathworks.com...
>> > dpb <none@non.net> wrote in message <lm20j1$1de$1@speranza.aioe.org>...
...

>> >> But, I'd rewrite the whole thing to be a little less verbose and
>> eliminate the string>number>string to just number>string...
>> >>
>> >> set(h,'xticklabel',num2str(get(h,'xtick').'*100,'%.1f%%'))
>> >>
...

>> >
>> > That removed the error, but created another problem, which I think
>> might > be related to what you stated last.
>> >
>> > My axes numbers seem to loop. 2% 4% 6% 8% 2% 4% and so on...
>>
>> My guess is that some of your subplots have fewer ticks than other
>> subplots and you're somehow using the number of ticks for one to
>> generate tick labels for another. But that's just a guess; post a
>> small but self-contained and executable section of code with which you
>> can reproduce the problem if you want the group to confirm or refute
>> that guess.
...

> I suspect you are right. Unfortunately your request is quite cumbersome,
> since I am importing data and the subplot is 4x2.
>
> If we assume you are right, can you send me in the right direction? I
> can then look at it myself?

The symptom of the repeating numbers is _very_ indicative of the problem
Steven outlined -- in that case you're writing to a plot that has more
than the number you've defined, so the ones provided are recycled to
match the number needed.

To fix the problem, loop over the number of subplots with the form I
provided -- it will retrieve the tick values from the present axes and
then rewrite the ticklabels formatted as desired.

You don't show the code where you created the subplots, but you need to
somehow save the axes handles in an array to use as the handle or simply
cycle thru each with the subplot() form to make it current and then can
use gca.

Either way, as noted, you have to make the number of labels consistent
with the number of ticks. My suggested way is to use the ticks as they
exist and label them. An alternative would be to make a consistent
number of ticks and labels but that requires resetting 'xtick' first in
case it isn't that standard (and perhaps checking on xlim, too, because
if the ticks aren't the same, chances are that's because the limits
aren't, either). Either works; you just have to decide which way you
want to do it.

--

Subject: Subplots with axis as percentages

From: Kelly Kearney

Date: 27 May, 2014 20:58:12

Message: 8 of 13

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm1l4n$b2t$1@newscl01ah.mathworks.com>...
> Hi,
>
> I am trying to change values on X-axis scatter plots to %, and I am using the following code:
>
> h=gca;
> labels=get(h,'xticklabel'); % get the x axis labels
> labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
> set(h,'xticklabel',labels_modif);
>
> This works perfectly when I work with scatters in isolation, but when I run my scatters as part of a subplot, things break down. I get the following error:
>
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.
>
> Error in ==> Figure at 80
> labels_modif=[num2str(100*str2num(labels)) ones(length(labels),1)*'%'];
>
> What is going wrong here?
>
> Regards Ulrik.

Perhaps this function could offer a shortcut?

http://www.mathworks.com/matlabcentral/fileexchange/16003-tick2text--create-easy-to-customize-tick-labels

I wrote it specifically because I got tired of keeping track of tick labels when trying to reformat lots of subplots.

Calling

  tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));

for each axis should accomplish the relabeling you need.

Subject: Subplots with axis as percentages

From: dpb

Date: 27 May, 2014 21:18:13

Message: 9 of 13

On 5/27/2014 3:58 PM, Kelly Kearney wrote:
...

> Calling
> tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));
>
> for each axis should accomplish the relabeling you need.

Well, that's just too kewl... :)

--

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 28 May, 2014 06:57:07

Message: 10 of 13

dpb <none@non.net> wrote in message <lm2ven$lm6$1@speranza.aioe.org>...
> On 5/27/2014 3:58 PM, Kelly Kearney wrote:
> ...
>
> > Calling
> > tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));
> >
> > for each axis should accomplish the relabeling you need.
>
> Well, that's just too kewl... :)
>
> --

Thank you for your time, dpb. Unfortunately I get the following message now:

Undefined function or method 'tick2text' for input arguments of type 'function_handle'.

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 28 May, 2014 07:03:08

Message: 11 of 13

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm41c3$nfa$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <lm2ven$lm6$1@speranza.aioe.org>...
> > On 5/27/2014 3:58 PM, Kelly Kearney wrote:
> > ...
> >
> > > Calling
> > > tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));
> > >
> > > for each axis should accomplish the relabeling you need.
> >
> > Well, that's just too kewl... :)
> >
> > --
>
> Thank you for your time, dpb. Unfortunately I get the following message now:
>
> Undefined function or method 'tick2text' for input arguments of type 'function_handle'.

Oh, it was a function from the File Exchange! :o) Thank you for your help. This worked. Although the tick marks and lables are not aligned, except at 0... I will look into that.

Subject: Subplots with axis as percentages

From: Ulrik Nash

Date: 28 May, 2014 07:09:08

Message: 12 of 13

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm41nc$o6m$1@newscl01ah.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm41c3$nfa$1@newscl01ah.mathworks.com>...
> > dpb <none@non.net> wrote in message <lm2ven$lm6$1@speranza.aioe.org>...
> > > On 5/27/2014 3:58 PM, Kelly Kearney wrote:
> > > ...
> > >
> > > > Calling
> > > > tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));
> > > >
> > > > for each axis should accomplish the relabeling you need.
> > >
> > > Well, that's just too kewl... :)
> > >
> > > --
> >
> > Thank you for your time, dpb. Unfortunately I get the following message now:
> >
> > Undefined function or method 'tick2text' for input arguments of type 'function_handle'.
>
> Oh, it was a function from the File Exchange! :o) Thank you for your help. This worked. Although the tick marks and lables are not aligned, except at 0... I will look into that.


I found the following in the exchange:

xtickinterval, ytickinterval... Problem solved!

Subject: Subplots with axis as percentages

From: Kelly Kearney

Date: 28 May, 2014 14:49:08

Message: 13 of 13

"Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm41nc$o6m$1@newscl01ah.mathworks.com>...
> "Ulrik Nash" <uwn@sam.sdu.dk> wrote in message <lm41c3$nfa$1@newscl01ah.mathworks.com>...
> > dpb <none@non.net> wrote in message <lm2ven$lm6$1@speranza.aioe.org>...
> > > On 5/27/2014 3:58 PM, Kelly Kearney wrote:
> > > ...
> > >
> > > > Calling
> > > > tick2text(h, 'axis', 'x', 'xformat', @(x) num2str(x*100, '%g%%'));
> > > >
> > > > for each axis should accomplish the relabeling you need.
> > >
> > > Well, that's just too kewl... :)
> > >
> > > --
> >
> > Thank you for your time, dpb. Unfortunately I get the following message now:
> >
> > Undefined function or method 'tick2text' for input arguments of type 'function_handle'.
>
> Oh, it was a function from the File Exchange! :o) Thank you for your help. This worked. Although the tick marks and lables are not aligned, except at 0... I will look into that.


Ulrick,

Could you email me the details of the plot where the tick marks and tick labels are not aligned? With tick2text, they certainly should be, especially if you used the syntax I suggested above (with the 'convert' option in its default false value). If there's a bug somewhere in my code, I'd like to correct it.

Thanks,
Kelly

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