发博文
正文 字体大小:

Matlab画图功能的补充

(2007-05-07 20:13:17)
标签:

软件

matlab

二维图形

功能

补充

分类: 软件
    下面的程序对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
        set(hlpp,'markeredgecolor','r');
        set(hlpp,'markerfacecolor','b');
        strp={['X=' num2str(px(np),'%8.4f')];['Y=' num2str(py(np),'%8.4f')]};
        htpp=text(px(np)+diff(axm)*0.01,py(np)-diff(aym)*0.03,strp);  % display the text of the peak
        set(htpp,'VerticalAlignment','top','HorizontalAlignment','left','color','r','edgecolor','g');
        recmp(np)=1;
        hlu{3}=recmp;
        set(obj,'userdata',hlu);
    end  
end


localpeak.m文件

function np=localpeak(px,py,ni)
% Find the local positive peak in py nearest to No.ni element
% Except the two ends
np=0;
if ni>1&&ni<length(px)
    na=0;nb=0;
    for k=ni:length(px)-1   % Search on the right
        if py(k)>py(k-1)&&py(k)>py(k+1)
            na=k;
            break;
        end
    end
    for k=ni:-1:2   %Search on the left
        if py(k)>py(k-1)&&py(k)>py(k+1)
            nb=k;
            break;
        end
    end
    if na*nb~=0  %Find the nearest
        if abs(px(na)-px(ni))<=abs(px(nb)-px(ni))
            np=na;
        else
            np=nb;
        end
    elseif na~=0
        np=na;
    elseif nb~=0
        np=nb;
    end
end

阅读 评论 收藏 转载 打印举报
  • 评论加载中,请稍候...

       

    验证码: 请点击后输入验证码 收听验证码

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有