Matlab 当颜色映射更改时执行函数
我正在开发一个图形用户界面控制MATLAB(2014a)程序,该程序有一个绘图窗口,在基于Matlab 当颜色映射更改时执行函数,matlab,plot,callback,matlab-guide,Matlab,Plot,Callback,Matlab Guide,我正在开发一个图形用户界面控制MATLAB(2014a)程序,该程序有一个绘图窗口,在基于pcolor的绘图上显示类似于轮廓的绘图 用户发现,右键单击颜色栏可以更改colormap。但是,由于mycontour函数的内部结构,此更改仅直接影响pcolor绘图 我已经了解了如何从Axis对象获取更改后的颜色贴图,并将其应用于等高线绘图,但我仍然必须手动重新绘制 当轴/图对象的颜色映射更改后,是否有任何回调执行 我读过,但是颜色映射似乎不是作为属性存储的。在Matlab pre-HG2中,您不需要借
pcolor
的绘图上显示类似于轮廓的绘图
用户发现,右键单击颜色栏可以更改colormap
。但是,由于mycontour
函数的内部结构,此更改仅直接影响pcolor
绘图
我已经了解了如何从Axis对象获取更改后的颜色贴图
,并将其应用于等高线
绘图,但我仍然必须手动重新绘制
当轴
/图
对象的颜色映射
更改后,是否有任何回调执行
我读过,但是颜色映射
似乎不是作为属性存储的。在Matlab pre-HG2中,您不需要借助未记录的功能来拦截颜色映射
更改。您只需将侦听器附加到属性'Colormap'
的'PostSet'
事件
举个简单的例子,如果您的图形已经存在,只需键入:
lh = addlistener( h.fig , 'Colormap' , 'PostSet' , @(h,e) disp('cmap changed !') )
在控制台中,每次更改colormap
时都会收到一条消息。请注意,事件将在以下情况下触发:
- 将
colormap
完全更改为另一个(例如从jet
更改为hsv
)
- 更改颜色贴图的大小(分割数)。(例如:
colormap(jet(5))
)
- 您可以使用“交互式彩色地图转换”gui工具
请注意,如果使用caxis
,事件将不会触发。此命令不会更改colormap
本身,而是更改某些颜色映射到它的方式。因此,如果使用此命令,您的pcolor
将被修改(尽管colormap不会被修改)。caxis
命令更改当前轴的CLim
属性(不是图
!)。因此,如果要检测到这一点,必须在正确的轴上将侦听器附加到该属性。比如:
lh = addlistener( gca , 'CLim' , 'PostSet' , @(h,e) disp('clim changed !') )
作为一个更实用的示例,这里有一个小演示,它将在每次更改colormap
时做出反应。由于我不知道每次更改时,您打算对等高线
绘图执行什么操作,因此我只修改了几个属性,以显示它正在执行某些操作。根据你需要做的调整
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
最近的MATLAB版本(HG-2之后)是否有解决办法?@oro777。很抱歉,我找不到一种快速的方法使其适用于最新的Matlab版本。