我有一些图形。用户可以删除任何选定的点。
我怎么知道用户删除了哪些点?“删除”是指使用 MATLAB 工具,例如“画笔/选择工具”,然后单击“删除”按钮。
如果您保存最初绘制的x
和y
数据,您可以将其与剩余的'XData'
或'YData'
在用户删除点后的图中进行比较,以确定哪些点被删除:
x = 1:10; %# The initial x data
y = rand(1,10); %# The initial y data
hLine = plot(x,y); %# Plot the data, saving a handle to the plotted line
%# ...
%# The user deletes two points here
%# ...
xRemaining = get(hLine,'XData'); %# Get the x data remaining in the plot
yRemaining = get(hLine,'YData'); %# Get the y data remaining in the plot
您在评论中提到您正在绘制 RR 间隔,因此您的x
数据应该是一个单调递增的时间点向量,没有重复值。因此,您可以通过执行以下操作找到删除的点:
removedIndex = ~ismember(x,xRemaining); %# Get a logical index of the points
%# removed from x
这为您提供了一个逻辑索引,其中一个表示已删除的点,而零表示仍然存在的点。如果用户仅删除了两个相邻点(如您所述,尽管您可能需要进行一些检查以确定),您可以轻松地用平均值替换这两个点,如下所示:
index = find(removedIndex); %# Get the indices from the logical vector
xNew = [x(1:index(1)-1) mean(x(index)) x(index(2)+1:end)]; %# New x vector
yNew = [y(1:index(1)-1) mean(y(index)) y(index(2)+1:end)]; %# New y vector
然后你可以相应地更新情节:
set(hLine,'XData',xNew,'YData',yNew);