[英]How to get coordinates of the clicked point when data cursor mode is on in matlab?
[英]MATLAB Data Cursor Mode
我有一個簡單的類,可以繪制基本的x和y數據。 在該類中,我有一個啟用數據光標模式,自定義文本以及收集和保存點的方法。 我想更改該方法的行為,以便一次只能收集兩個點。 現在,即使我關閉了數據光標模式並重新打開以使用它,它也會存儲每個點。 這是我上課的代碼:
classdef CursorPoint
properties
Xdata
Ydata
end
methods
function me = CursorPoint(varargin)
x_data = 0:.01:2*pi;
y_data = cos(x_data);
f= figure;
plot(x_data,y_data);
me.DCM(f);
end
function DCM(me,fig)
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn)
set(dcm_obj,'enable','on')
myPoints=[];
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
pos = get(event_obj,'Position');
myPoints(end + 1,:) = pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end
end
end
end
您能否將myPoints
變量更改為兩個名為myPointCurrent
和myPointPrevious
變量。 每當調用myupdatefcn
方法時,您都將myPointCurrent
的內容移動到myPointPrevious
,然后將當前位置存儲在myPointCurrent
。
新功能(帶有一些錯誤檢查)將類似於:
function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
myPointPrevious=myPointCurrent;
pos = get(event_obj,'Position');
myPointCurrent=pos;
txt = {['Time: ',num2str(pos(1))],...
['Amplitude: ',num2str(pos(2))]};
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.