How can I drag points vertically in a plot?
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Azevedo
am 26 Mär. 2018
Kommentiert: ska109
am 20 Feb. 2025
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 Kommentare
Akzeptierte Antwort
Akira Agata
am 26 Mär. 2018
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 Kommentare
Panya Kansuwan
am 6 Jul. 2022
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
am 20 Feb. 2025
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!