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.

 채택된 답변

Akira Agata
Akira Agata 2018년 3월 26일

8 개 추천

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

Very nice. Thanks.
Hi Akira,
I'm working on some similar code and just found this. It looks brilliant but I'm struggling to understand how it works. I can't put breaks on it and go line by line to understand it for some reason.
Perhaps you could help me.
I'd like to modify the code you already wrote such when the user releases the mouse after moving a data point, the new Yvalues (after the user has altered them) are output to the MATLAB workspace.
Thank you!
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
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개)

카테고리

태그

질문:

2018년 3월 26일

댓글:

2025년 2월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by