MATLAB动态绘图

时间:2015-05-19 14:16:01

标签: matlab zooming

我编写了一个简单的类,它创建了两个具有时间和值数组的属性。该类具有简单绘制它们的方法。此外,还有一些方法可以在绘图上侦听缩放事件,并根据缩放限制计算绘图的平均值。但是,它目前只显示我放在图中的文本编辑框中的平均值;我真正喜欢的是动态绘制计算出的平均值。以下是我的代码:

classdef ZoomPlot < handle

properties

    PropX
    PropY

end

properties (SetObservable)
    MasterXAxis = [];
    NewAverage = '';
end

methods
    % -----------------------------------------------------------------
    function me = ZoomPlot(varargin)
        if nargin == 0

            t = 100:220;
            x1 = -(10*(t-130)).^2;
            x2 = -(10*(t-150)).^2;
            me.MasterXAxis = t;
            me.PropX = struct('Time',t,'Value',x1);
            me.PropY = struct('Time',t,'Value',x2);
        end
    end
    % -----------------------------------------------------------------
    function plotPropX(me)

        x = me.PropX;
        plot(x.Time,x.Value)

        h = zoom;
        set(h,'ActionPostCallback',@me.ZoomLims);

        me.attachListener;

    end
    % -----------------------------------------------------------------
    function plotPropY(me)

        y = me.PropY;
        figure;
        plot(y.Time,y.Value)

        h = zoom;
        set(h,'ActionPostCallback',@me.ZoomLims);

        me.attachListener;

    end
    % -----------------------------------------------------------------
    function ZoomLims(me,obj,evd)
        %
        xlims = get(evd.Axes,'XLim');
        me.MasterXAxis = get(evd.Axes,'XLim');
        ylims = get(evd.Axes,'Ylim');
        xdat = get(get(evd.Axes,'children'),'XData');
        ydat = get(get(evd.Axes,'children'),'YData');


        zoomed_x = xdat(find(xdat>=xlims(1)&xdat<=xlims(2)));
        zoomed_y = ydat(find(ydat>=ylims(1)&ydat<=ylims(2)));

        temp_mean = mean(zoomed_y);

        me.NewAverage = temp_mean;            
    end
    % -----------------------------------------------------------------
    function attachListener(me)

        addlistener(me,{'MasterXAxis','NewAverage'},'PostSet',@Material.propChange);            
    end
end
methods (Static)
    % -----------------------------------------------------------------
    function h = propChange(metaProp,eventData)
        % Callback for PostSet event
        % Inputs: meta.property object, event.PropertyEvent

        h = eventData.AffectedObject;
        h.MasterXAxis;
        h.NewAverage;
        MyBox = uicontrol('style','text','Units','normalized','Position', [0.3 0.06 0.1 0.025]);
        MyAverageTitle = uicontrol('style','text','Units','normalized','Position', [0.15 0.06 0.1 0.025]);
        set(MyBox,'String',num2str(h.NewAverage));
        set(MyAverageTitle,'String','Average dB');

        propName = metaProp.Name;
        disp(['The ',propName,' property has changed.'])
        disp(['The new values are: ',num2str(h.MasterXAxis)])
        disp(['Its default value is: ',num2str(metaProp.DefaultValue)])
    end
    % -----------------------------------------------------------------
end

使用示例:

zp = ZoomPlot()
zp.plotPropX
zp.plotPropY

欢迎任何其他批评以最佳地优化代码/架构。谢谢。

0 个答案:

没有答案