How do I implement Drag and Drop functionality in MATLAB?

50 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2012 年 8 月 17 日
編集済み: MathWorks Support Team 2023 年 4 月 27 日
I would like to be able to drag and drop a graphic object (Eg : TEXTBOX) across a figure window.

採用された回答

MathWorks Support Team
MathWorks Support Team 2019 年 4 月 12 日
編集済み: MathWorks Support Team 2019 年 4 月 12 日
In Image Processing Toolbox, there are some functions which provides draggable object such as imrect and impoly etc.
For details, please refer the following documentation:
Also, this feature can be implemented using a combination of the Figure Properties and the graphic object properties. The figure properties to be used are:
WindowButtonUpFcn
WindowButtonMotionFcn
CurrentPoint
The property of the graphic object to be used is:
ButtonDownFcn
The following example implements drag and drop functionality for a text box that is displayed on a Figure Window. This example function is also attached as a MATLAB-file:
function drag_drop
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
a = annotation('textbox','position',[0.2 0.2 0.2 0.2],'String','Hello','ButtonDownFcn',@dragObject);
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end
  1 件のコメント
Abhishek Pandey
Abhishek Pandey 2016 年 6 月 6 日
編集済み: MathWorks Support Team 2023 年 4 月 27 日
Hi Mariana,
If you still need help with this, I would recommend contacting MathWorks Technical Support.
- Abhishek

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGPU Computing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by