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

New to MATLAB?

Thread Subject:
Copying colorbars and colorbar UI panel button

Subject: Copying colorbars and colorbar UI panel button

From: HJ

Date: 18 Feb, 2014 23:31:08

Message: 1 of 2

I have a callback triggered by WindowButtonDownFcn that copies a 2D scatter plot from the selected (clicked-on) axes to a new set of axes in a new figure:

% main script
hfig = figure('WindowButtonDownFcn', @myCallback);
subplot(211); h1=scatter(...); colorbar('peer', h1);
subplot(212); h2=scatter(...); colorbar('peer', h2);

% myCallback.m
function myCallback(hObj, eventData)
figAxes = findobj(hObj,'type','axes','-not','tag','Colorbar');
hfig2 = figure;
idxAxes = find(...); % figure out which axes to copy, e.g. using GCO
h3=copyobj(figAxes(idx), hfig2);

Since colorbar objects are rendered as standalone axes and not automatically copied by COPYOBJ, I have to copy them separately:

% again in myCallback.m ...
colorbarAxes = findobj(hObj,'type','axes','tag','Colorbar');
idxColorbar = find(...); % figure out corresponding colorbar object
h4=copyobj(colorbarAxes(idxColorbar), hfig2);

The problem with the above is that the copied colorbar does not seem to be "linked" to the copied scatter plot in hfig2. In particular, if I click the colorbar UI button at the top of the Figure 2 window, I get a new colorbar to the right of the axes but to the left of the copied colorbar. Evidently MATLAB treats the copied colorbar as just another axes in the figure window, and not as one having a specific relationship to the contents of the axes.

Is there a clean way to establish such a link? The only other way I can think of, which is a bit cumbersome, is to do something like h4=colorbar('peer', h3), and then to manually copy the relevant properties (CLim, etc.) from the original colorbar (idxColorbar) to the new one (h4)?

Thanks in advance for any suggestions,


Subject: Copying colorbars and colorbar UI panel button

From: HJ

Date: 19 Feb, 2014 02:18:08

Message: 2 of 2

Answering my own question...

It looks like what I'm seeing is a minor rendering artifact/bug in R2006b, which I'm stuck using at the moment. I assume that it has been fixed by now. In any case, I found that the issue is resolved by calling


at the end of the callback function. This gets rid of the extra colorbar, and leaves the figure window with the colorbar UI button selected, as expected (since the colorbar is shown). Toggling that button makes the colorbar go away and come back, just as it should.


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