tags:

views:

377

answers:

2

I have a problem with editing the colorbar in MATLAB. The colorbar is drawn and I want to add the unit (dB) for the specific measurement on YTickLabels. This is done by following commands:

cy = get(ch,'YTickLabel');  
set(ch,'YTickLabel',[]);  
set(ch,'YTickLabel',strcat(cy,{' dB'})); 

But when I resize the figure, MATLAB redefines the intervals, and the output is repeated twice, like:

10 dB, 20 dB, 30 dB, 10 dB, 20 dB, 30 dB instead of 10 dB, 20 dB, 30 dB.

How do I prevent MATLAB from redefining its Y axis ticks, so it doesn't mess up my colorbar?

A: 

You have to set manually YTick property as well, so it will not change with figure resize.

cytick = get(ch,'YTick');    
set(ch,'YTick',cytick);
yuk
+3  A: 

In order to keep the y-axis tick values from being changed when the figure is resized, you will have to either explicitly set the 'YTick' property or set the 'YTickMode' property to 'manual' (to keep it from being automatically changed). You may also have to explicitly set the 'YLim' property as well (or set the 'YLimMode' property to 'manual') to keep the limits of the color bar from changing. Here's one possible solution:

labels = get(ch,'YTickLabel');    %# Get the current labels
set(ch,'YLimMode','manual',...    %# Freeze the current limits
       'YTickMode','manual',...   %# Freeze the current tick values
       'YTickLabel',strcat(labels,{' dB'}));  %# Change the labels

You can also define the tick properties when you create the color bar in your initial call to the COLORBAR function. For example, if you know you will want to have 3 tick values at 10, 20, and 30 with "dB" added to the labels, you can create the color bar in the following way:

ch = colorbar('YLim',[10 30],...                        &# The axis limits
              'YTick',[10 20 30],...                    %# The tick locations
              'YTickLabel',{'10 dB','20 dB','30 dB'});  %# The tick labels

These limits, tick values, and tick labels should also remain unchanged when the figure is resized.

gnovice
If I get the colorbar handler to ch, then I can't use the colorbar instead of set, because it must be followed by either 'delete', 'hide' or 'off'. When I use the set function it works perfectly. So thanks.
Soren M
@Soren: Hmmm, maybe there's a version issue with regard to using COLORBAR instead of SET (perhaps it's specific to newer versions). I'll update the answer to just use SET so no one else runs into the same problem.
gnovice

related questions