MATLAB动态绘图
我编写了一个简单的类,它创建了两个具有时间和值数组的属性。该类具有简单地绘制它们的方法。此外,还有一些方法可以侦听绘图上的缩放事件,并根据缩放限制计算绘图的平均值。但是,它目前只在我放在图上的文本编辑框中显示平均值;我真正想要的是它动态地绘制计算出的平均值。下面是我的代码:MATLAB动态绘图,matlab,zooming,Matlab,Zooming,我编写了一个简单的类,它创建了两个具有时间和值数组的属性。该类具有简单地绘制它们的方法。此外,还有一些方法可以侦听绘图上的缩放事件,并根据缩放限制计算绘图的平均值。但是,它目前只在我放在图上的文本编辑框中显示平均值;我真正想要的是它动态地绘制计算出的平均值。下面是我的代码: classdef ZoomPlot < handle properties PropX PropY end properties (SetObservable) MasterXAxis
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
也欢迎任何其他评论,以最佳地优化代码/体系结构。谢谢 也许你可以在绘制绘图时为区域平均值添加一条空线,然后在限制改变时更新相关数据-我将把refreshdata命令放在哪里?我自己没有使用过它,所以我不知道它是否能满足你的需要(因此,注释不是答案),只是玩一玩,看看它是否适合你的目的。至于它去哪里。。。如果您考虑该函数的功能以及您想要实现的功能,那么这一点应该变得显而易见:)
zp = ZoomPlot()
zp.plotPropX
zp.plotPropY