How can I drag points vertically in a plot?
조회 수: 18 (최근 30일)
이전 댓글 표시
Given a plot, I want to select a point in the plot and then drag it vertically in the plot using the mouse cursor.
Here is an example of my initial plot:

After drag the point it should look like this:

I already tried to use the brush function, but the best I can do is to get the coordinates of the selected points, but then I don’t know how to do the drag part.
댓글 수: 0
채택된 답변
Akira Agata
2018년 3월 26일
The solution would be like this. Please save the following code as an script file, and run it.
% Plot a line and points
figure
plot(1:10,1:10,'-o','buttondownfcn',{@Mouse_Callback,'down'});
% Callback function for each point
function Mouse_Callback(hObj,~,action)
persistent curobj xdata ydata ind
pos = get(gca,'CurrentPoint');
switch action
case 'down'
curobj = hObj;
xdata = get(hObj,'xdata');
ydata = get(hObj,'ydata');
[~,ind] = min(sum((xdata-pos(1)).^2+(ydata-pos(3)).^2,1));
set(gcf,...
'WindowButtonMotionFcn', {@Mouse_Callback,'move'},...
'WindowButtonUpFcn', {@Mouse_Callback,'up'});
case 'move'
% vertical move
ydata(ind) = pos(3);
set(curobj,'ydata',ydata)
case 'up'
set(gcf,...
'WindowButtonMotionFcn', '',...
'WindowButtonUpFcn', '');
end
end
댓글 수: 4
Panya Kansuwan
2022년 7월 6일
Hi Akira,
Thanks for your answer. I have the same lase question as Gregory smith. Would you please inform how we can get updated y-data in workspace?
Best regards
ska109
2025년 2월 20일
I have modified Akira's answer in such way, that all parts of the code are functions. It's the similar way as GUIDE works. Data are exported to Workspace using ASSIGNIN function.
function Klikatko() % Based on https://www.mathworks.com/matlabcentral/answers/390704-how-can-i-drag-points-vertically-in-a-plot#answer_312006
if exist('hObject') == 0
% Inicializace
close all; clc;
handles = [];
handles.pl = [];
handles.plm = [];
% Plot a line and points
handles.figure_hlavni = figure;
handles.pl = plot(-3:3, (-3:3).^3, '-o');
handles.axis_hlavni = gca;
else
handles = guidata(hObject);
end
ExportData(handles.pl)
set(handles.pl, 'buttondownfcn', @Mouse_Callback_Down);
guidata(handles.figure_hlavni, handles);
end
function Mouse_Callback_Down(hObject,~,action)
handles = guidata(hObject);
xdata = get(handles.pl, 'xdata');
ydata = get(handles.pl, 'ydata');
pos = get(handles.axis_hlavni, 'CurrentPoint');
[~, handles.ind] = min(sum((xdata-pos(1, 1)).^2+(ydata-pos(1, 2)).^2, 1)); % Minimum součtu čtverců odchylek
delete(handles.plm);
hold on; handles.plm = plot(xdata(handles.ind), ydata(handles.ind), 'r-o');
set(handles.figure_hlavni,...
'WindowButtonMotionFcn', @Mouse_Callback_Move, ...
'WindowButtonUpFcn', @Mouse_Callback_Up);
guidata(handles.figure_hlavni, handles);
end
function Mouse_Callback_Move(hObject,~,action)
handles = guidata(hObject);
xdata = get(handles.pl, 'xdata');
ydata = get(handles.pl, 'ydata');
pos = get(handles.axis_hlavni, 'CurrentPoint');
ydata(handles.ind) = pos(1, 2);
set(handles.pl, 'ydata', ydata);
delete(handles.plm);
hold on; handles.plm = plot(xdata(handles.ind), ydata(handles.ind), 'r-o');
ExportData(handles.pl)
guidata(handles.figure_hlavni, handles);
end
function Mouse_Callback_Up(hObject,~,action)
handles = guidata(hObject);
set(handles.figure_hlavni,...
'WindowButtonMotionFcn', '',...
'WindowButtonUpFcn', '');
ExportData(handles.pl)
end
function ExportData(pl)
Data(1, :) = get(pl, 'xdata');
Data(2, :) = get(pl, 'ydata');
assignin('base', 'Data', Data); % https://www.mathworks.com/matlabcentral/answers/96201-how-do-i-access-a-base-workspace-variable-from-within-a-matlab-function
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!