下面的程序对Matlab的画图功能进行了二次开发。包括在Matlab的fig文件中对二维图形(可以是多条曲线)标注最大值、动态显示当前鼠标在曲线点击位置的坐标,同时显示曲线上靠近点击处最近的局部极大值点。这些补充功能对于评估测试数据非常有用。包括三个文件:
"extradisplay.m","LineBtn.m","localpeak.m" 其中"extradisplay.m"是主文件,"LineBtn.m"实现对鼠标点击的响应,"localpeak.m"寻找距点击位置最近的局部极大值。
----------------------------------------------------------------------
extradisplay.m文件
% This program provides extra display
functions for a matlab 2-D figure
% including the maximum, the prompt of the value at the cursor and
the
% local positive peak nearest to the point where the button presses
down.
% It is especially convenient to find the peaks
interactively.
% The following code referred to the similar program by the
Author
% WaitingForMe whose Email is heroaq_2002@163.com.
strfig=input('Enter the fig file
name:\n
','s');
hf=openfig(strfig,'new');
hls=findobj(hf,'Type','line'); % Handles of lines in the
figure
set(hls,'ButtonDownFcn',{'LineBtn',hf});
LineBtn.m文件
function LineBtn(obj,eventdata,hf)
% Callback for the ButtunDownFcn, and no return value
% obj is the handle of the clicked curve
if strcmp(get(hf,'SelectionType'),'normal')
px=get(obj,'xdata');
py=get(obj,'ydata');
ha=get(obj,'Parent'); % handle of the current
axes
axm=get(ha,'XLim'); % the limit of X axis
aym=get(ha,'YLim'); % the limit of Y axis
axes(ha);
cr=1-get(obj,'color'); % define the inverse color of the line
% Label the
maximum for the current curve
if
length(get(obj,'userdata'))==0
% Test if the curve has been labeled the maximum, or xor will erase
it.
[my,ni]=max(py);
mx=px(ni);
hlm=line(mx,my,'marker','d','markersize',6,'LineWidth',2,'erasemode','xor');
% marker of the maximum of y
set(hlm,'markeredgecolor',1-cr);
set(hlm,'markerfacecolor',cr);
strm={['X=' num2str(mx,'%8.4f')];['Y=' num2str(my,'%8.4f')]};
htm=text(mx-diff(axm)*0.01,my-diff(aym)*0.03,strm);
% display the text of the maximum
set(htm,'VerticalAlignment','top','HorizontalAlignment','left','color','r','edgecolor',cr);
recm=zeros(1,length(px));
recm(ni)=1; % Record the marked element for the
curve
set(obj,'userdata',{hlm,htm,recm});
end
% Marked the
current cursor
pos=get(ha,'CurrentPoint');
[tx,ni]=min(abs(px-pos(1)));
dpos=[px(ni),py(ni)];
if
length(get(ha,'userdata'))==0
hlp=line(dpos(1),dpos(2),'marker','s','markersize',6,'LineWidth',2,'erasemode','xor');
% marker of the cursor
set(hlp,'markeredgecolor','r');
set(hlp,'markerfacecolor','g');
strp={['X=' num2str(dpos(1),'%8.4f')];['Y='
num2str(dpos(2),'%8.4f')]};
htp=text(dpos(1)+diff(axm)*0.01,dpos(2)+diff(aym)*0.01,strp);
% display the text of the maximum
set(htp,'VerticalAlignment','bottom','HorizontalAlignment','left','color','k','edgecolor','b');
set(ha,'userdata',[hlp,htp]);
else
hau=get(ha,'userdata');
hlp=hau(1);htp=hau(2);
set(hlp,'xdata',dpos(1),'ydata',dpos(2));
strp={['X=' num2str(dpos(1),'%8.4f')];['Y='
num2str(dpos(2),'%8.4f')]};
set(htp,'Position',[dpos(1)+diff(axm)*0.01,dpos(2)+diff(aym)*0.01],'string',strp);
end
% Marked the
neighbouring positive peak
np=localpeak(px,py,ni);
hlu=get(obj,'userdata');
recmp=hlu{3};
if
np~=0&&recmp(np)==0
hlpp=line(px(np),py(np),'marker','p','markersize',6,'LineWidth',2,'erasemode','xor');
% marker of the peak