You do not need to resort to undocumented functions to catch the colormap change in Matlab pre-HG2. You can simply attach a listener to the 'PostSet' event of the 'PostSet' property.
As a quick example, if your digit already exists, simply enter:
lh = addlistener( h.fig , 'Colormap' , 'PostSet' , @(h,e) disp('cmap changed !') )
in the console and you will get a message every time the colormap changes. Note that the event is triggered:
- you completely change
colormap to another (e.g. from jet to hsv ) - you change the size of the color map (number of divisions). (e.g.
colormap(jet(5)) ) - you are using the Gui-Interactive colormap shift tool.
Please note that the event will not be if you use caxis . This command does not change the colormap itself, but rather a way of matching some colors. Therefore, if you use this command, your pcolor will be changed (although the color palette will not). The caxis command changes the caxis property of the current axes (not figure !). Therefore, if you want to detect this, you must attach the listener to this property on the correct axis. Sort of:
lh = addlistener( gca , 'CLim' , 'PostSet' , @(h,e) disp('clim changed !') )
As a more illustrative example, here is a small demo that will react every time a colormap changes. Since I donβt know what you planned to do in your contour plot with each change, I just change a couple of properties to show that he is doing something. Adjust it as you need.
function h = cmap_change_event_demo %// SAMPLE DATA. create a sample "pcolor" and "contour" plot on a figure nContour = 10 ; [X,Y,Z] = peaks(32); h.fig = figure ; h.pcol = pcolor(X,Y,Z) ; hold on; [~,h.ct] = contour(X,Y,Z,nContour,'k'); h.cb = colorbar shading interp colormap( jet(nContour+1) ) %// assign a colormap with only 10+1 colors %// add the listener to the "Colormap" property h.lh = addlistener( h.fig , 'Colormap' , 'PostSet' , @cmap_changed_callback ) %// save the handle structure guidata( h.fig , h ) function cmap_changed_callback(~,evt) %// disp('cmap changed !') hf = evt.AffectedObject ; %// this is the handle of the figure cmap = evt.NewValue ; %// this is the new colormap. Same result than : cmap = get(hf,'Colormap') ; h = guidata( hf ) ; %// to retrieve your contour handles (and all the other handles) nColor = size(cmap,1) ; %// to know how many colors are in there if you want matching contours %// or just do something useless set(h.ct , 'LineColor' , rand(1,3) ) %// change line color set(h.ct , 'LineWidth' , randi([1 5],1) ) %// change line thickness
