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?
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.
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);
精彩评论