MATLAB: Copying colorbar from one figure to another and replacing first YTick

colorbarimagesclogarithmicytick

Hi. I have a 60×60 matrix "count" (containing values between 0 and >1000) that I would like to make an imagesc from, using a logarithmic scale, by first creating a non logarithmic one with a logarithmic colorbar and then creating a logarithmic one and copying the logarithmic colorbar from the first one (so the colors in the image and the colorbar match the values).
Here is my attempt so far
figure(1) %first figure
count2=count+1; %add 1, because log10(0) not defined
imagesc(count2') %image with real values
cbr = colorbar('Yscale', 'log'); %logarithmic colorbar
figure(2) %second figure
imagesc(log10(count2')) %logarithmic image
set(gca,'YDir','normal')
a = get(1,'children'); %extract colorbar from first figure
copyobj(a(1),2); %copy to second figure
close Figure 1
The problem is that the colours from the image and the colorbar match, but the colorbar covers part of the figure. I would like the second figure to resize when copying the colorbar. Also, since I had to add 1 to count for the logarithmic scaling, I would like to replace the YTick 10^0 at the bottom of the colorbar with 0. For the others YTicks it doesn't matter if the accuracy is +- 1.
Thanks for any help!

Best Answer

Okay, I found a workaround using xlim, removing the border and making the background white. There's still a little bit of the x-axis behind the colorbar, but it looks decent:
figure(1) %first figure
count2(count2 == 0) = 1; %log(0) not defined - replace all zeros in count
imagesc(count2') %real values, but "wrong" colormap
cbr = colorbar('Yscale', 'log'); %logarithmic colorbar
figure('Color',[1 1 1]) %second figure with white background
imagesc(log10(count2')) %logarithmic image with colormap that fits colorbar 1
set(gca,'YDir','normal') %flip image
a = get(1,'children'); %extract colorbar from figure 1
copyobj(a(1),2); %copy to figure 2
close Figure 1
xlim([1 67]) %shrink imgage, so that colorbar doesn't overlap
box off %rempove frame (that still overlaps colorbar)