MATLAB:即使在平移等之后,如何从时间序列图中连续存储点击坐标(峰值点)
MATLAB: How to store clicked coordinates (peakpoints) continuosly from time series plot even after panning etc.
我目前正在使用 matlab GUI,我需要单击不需要的峰值点(而不是删除它们)并将它们的结果存储在矩阵中(连续)。我正在使用带有以下代码的按钮来收集点数并存储结果。但是,当我单击这些点时,只会存储最后单击的结果(而不是所有单击的点)。此外,由于它是一个连续的图,我使用 PAN 按钮来移动数据。因此,我想做以下事情:
1) 对于按钮单击(从 getpts 函数收集峰值)我想单击并收集几个点(为每次单击连续存储和附加值)。此外,即使在我使用 PAN 按钮移动绘图后,我也希望阵列处于活动状态。
2)我想创建另一个按钮来结束任务(要求用户"do you want to stop collecting the peak points",停止收集点并将整个点击结果存储在一个数组中)
axes(handles.axes1);
[ptsx1,ptsy1] = getpts(gcf);
idx = knnsearch([t',fbsum],[ptsx1 ptsy1],'k',1)
if evalin('caller', 'exist(''xArray'',''var'')')
xArray = evalin('caller','xArray');
else
xArray = [];
end
xArray = [xArray; idx] %
assignin('caller','xArray',xArray); `% save to base`
save('xArray.mat','xArray');
抱歉,这是我的第一个post,请接受我的道歉,如果有什么不清楚的地方请澄清。提前致谢。
编辑:改用基于 GUIDE 的 GUI
在 Matlab 命令中 window 定义你的 X 和 Y
>> x = 0:.1:4*pi;
>> y = sin(x);
>> y(10:10:end) = 2; %With some bad points to mark
然后 运行 GUI,select 开始标记,标记一些点 Pan around 等等:
>> plotTest(x,y)
退出 GUI 后查看 globla IDX_STORE:
>> global IDX_STORE
>> IDX_STORE
IDX_STORE =
10 30 40
如果你想要标记 x , y 值那么它只是
>> markedX = x(IDX_STORE);
>> markedY = y(IDX_STORE);
GUI 的布局类似于 this picture。
GUI 代码如下所示:
function varargout = plotTest(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',mfilename,'gui_Singleton',gui_Singleton,'gui_OpeningFcn', @plotTest_OpeningFcn, ...
'gui_OutputFcn', @plotTest_OutputFcn,'gui_LayoutFcn',[] ,'gui_Callback',[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before plotTest is made visible.
function plotTest_OpeningFcn(hObject, eventdata, handles, varargin)
global IDX_STORE
IDX_STORE = [];
handles.output = [];
if numel(varargin) ~= 2
handles.closeFigure = true;
else
x = varargin{1}; y =varargin{2};
handles.l = plot(handles.axes1,x,y);
hold(handles.axes1,'on')
handles.axes1.ButtonDownFcn = {@clickCallback,handles};
handles.l.ButtonDownFcn = {@clickCallback,handles};
guidata(hObject, handles);
end
% --- Outputs from this function are returned to the command line.
function varargout = plotTest_OutputFcn(hObject, eventdata, handles)
varargout{1} = [];
if (isfield(handles,'closeFigure') && handles.closeFigure)
errordlg('Nothing Passed in!')
end
% --- Executes on button press in markToggle.
function markToggle_Callback(hObject, eventdata, handles)
switch handles.markToggle.Value
case 1
handles.markToggle.String = 'Stop Marking';
case 0
handles.markToggle.String = 'Start Marking';
end
function clickCallback(hObj,evtData,handles)
global IDX_STORE
if handles.markToggle.Value == 0
return %Do Nothing if toggle not pressed.
end
coordinates = handles.axes1.CurrentPoint(1,1:2); %Get coordinates of mouse click
idx = knnsearch([handles.l.XData' handles.l.YData'],coordinates);%Find closest point in line data
IDX_STORE = unique([IDX_STORE idx]); %Store the index.
mH = findobj(handles.axes1,'tag','markLine');%Add some markers to see what you are doing.
if isempty(mH) %Make the marker plot if it doesn't exist
plot(handles.axes1, handles.l.XData(IDX_STORE),handles.l.YData(IDX_STORE),'rO','tag','markLine')
else%If it does exist then update the markers
mH.XData = handles.l.XData(IDX_STORE); mH.YData = handles.l.YData(IDX_STORE);
end
guidata(hObj,handles); %Save handles structure
我目前正在使用 matlab GUI,我需要单击不需要的峰值点(而不是删除它们)并将它们的结果存储在矩阵中(连续)。我正在使用带有以下代码的按钮来收集点数并存储结果。但是,当我单击这些点时,只会存储最后单击的结果(而不是所有单击的点)。此外,由于它是一个连续的图,我使用 PAN 按钮来移动数据。因此,我想做以下事情:
1) 对于按钮单击(从 getpts 函数收集峰值)我想单击并收集几个点(为每次单击连续存储和附加值)。此外,即使在我使用 PAN 按钮移动绘图后,我也希望阵列处于活动状态。 2)我想创建另一个按钮来结束任务(要求用户"do you want to stop collecting the peak points",停止收集点并将整个点击结果存储在一个数组中)
axes(handles.axes1);
[ptsx1,ptsy1] = getpts(gcf);
idx = knnsearch([t',fbsum],[ptsx1 ptsy1],'k',1)
if evalin('caller', 'exist(''xArray'',''var'')')
xArray = evalin('caller','xArray');
else
xArray = [];
end
xArray = [xArray; idx] %
assignin('caller','xArray',xArray); `% save to base`
save('xArray.mat','xArray');
抱歉,这是我的第一个post,请接受我的道歉,如果有什么不清楚的地方请澄清。提前致谢。
编辑:改用基于 GUIDE 的 GUI
在 Matlab 命令中 window 定义你的 X 和 Y
>> x = 0:.1:4*pi;
>> y = sin(x);
>> y(10:10:end) = 2; %With some bad points to mark
然后 运行 GUI,select 开始标记,标记一些点 Pan around 等等:
>> plotTest(x,y)
退出 GUI 后查看 globla IDX_STORE:
>> global IDX_STORE
>> IDX_STORE
IDX_STORE =
10 30 40
如果你想要标记 x , y 值那么它只是
>> markedX = x(IDX_STORE);
>> markedY = y(IDX_STORE);
GUI 的布局类似于 this picture。
GUI 代码如下所示:
function varargout = plotTest(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',mfilename,'gui_Singleton',gui_Singleton,'gui_OpeningFcn', @plotTest_OpeningFcn, ...
'gui_OutputFcn', @plotTest_OutputFcn,'gui_LayoutFcn',[] ,'gui_Callback',[]);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before plotTest is made visible.
function plotTest_OpeningFcn(hObject, eventdata, handles, varargin)
global IDX_STORE
IDX_STORE = [];
handles.output = [];
if numel(varargin) ~= 2
handles.closeFigure = true;
else
x = varargin{1}; y =varargin{2};
handles.l = plot(handles.axes1,x,y);
hold(handles.axes1,'on')
handles.axes1.ButtonDownFcn = {@clickCallback,handles};
handles.l.ButtonDownFcn = {@clickCallback,handles};
guidata(hObject, handles);
end
% --- Outputs from this function are returned to the command line.
function varargout = plotTest_OutputFcn(hObject, eventdata, handles)
varargout{1} = [];
if (isfield(handles,'closeFigure') && handles.closeFigure)
errordlg('Nothing Passed in!')
end
% --- Executes on button press in markToggle.
function markToggle_Callback(hObject, eventdata, handles)
switch handles.markToggle.Value
case 1
handles.markToggle.String = 'Stop Marking';
case 0
handles.markToggle.String = 'Start Marking';
end
function clickCallback(hObj,evtData,handles)
global IDX_STORE
if handles.markToggle.Value == 0
return %Do Nothing if toggle not pressed.
end
coordinates = handles.axes1.CurrentPoint(1,1:2); %Get coordinates of mouse click
idx = knnsearch([handles.l.XData' handles.l.YData'],coordinates);%Find closest point in line data
IDX_STORE = unique([IDX_STORE idx]); %Store the index.
mH = findobj(handles.axes1,'tag','markLine');%Add some markers to see what you are doing.
if isempty(mH) %Make the marker plot if it doesn't exist
plot(handles.axes1, handles.l.XData(IDX_STORE),handles.l.YData(IDX_STORE),'rO','tag','markLine')
else%If it does exist then update the markers
mH.XData = handles.l.XData(IDX_STORE); mH.YData = handles.l.YData(IDX_STORE);
end
guidata(hObj,handles); %Save handles structure