Matlab drag file from Explorer window onto digit (gui)

I would like to know that there is a way to drag and drop a file from Windows Explorer and delete it in my GUI.

The goal should be to get the file path (or folder path) and be able to download it using my own upload function.

I know for sure that I use Matlab 2015b on Windows 10 64 bit.

I am editing my post to give an example of the code of what I'm trying to do (based on a solution by Yair Altman and others found on the Internet):

function demo % Set-up a figure droppable axis hFig = figure('name','DND example','numbertitle','off'); hAx1 = axes('position',[.1,.1,.8,.8]); % Enable drop on the figure axis dnd = handle(java.awt.dnd.DropTarget(),'callbackProperties'); jFrame = get(hFig,'JavaFrame'); jAxis = jFrame.getAxisComponent; jAxis.setDropTarget(dnd); set(dnd,'DropCallback',{@dndCallbackFcn,hFig, hAx1}); set(dnd,'DragOverCallback',@dndCallbackFcn); end function dndCallbackFcn(varargin) persistent transferable eventData = varargin{2}; if eventData.isa('java.awt.dnd.DropTargetDropEvent') %nargin>2 hFig = varargin{3}; % my figure is passed as the third argument try eventData.acceptDrop(eventData.getDropAction); transferable = eventData.getTransferable; catch end dataFlavorList = java.awt.datatransfer.DataFlavor.javaFileListFlavor; fileList = transferable.getTransferData(dataFlavorList); %{ I want here to get back the file path and then call my loading function %} end end 

I always get an error in the line:

 fileList = transferable.getTransferData(dataFlavorList); 

The error is as follows:

 Java exception occurred: java.awt.dnd.InvalidDnDOperationException: No drop current at sun.awt.dnd.SunDropTargetContextPeer.getTransferData(Unknown Source) at sun.awt.datatransfer.TransferableProxy.getTransferData(Unknown Source) at java.awt.dnd.DropTargetContext$TransferableProxy.getTransferData(Unknown Source) 
+1
source share
2 answers

I tried to implement the same functions as yours and fell into the same exceptions when trying to get the transmitted data.

It is unclear if getTransferable fails due to the default FlavorMap created in %matlabroot%\sys\java\jre\...\lib\flavormap.properties (as indicated in the book by Yair Altman in the drag and drop section) or some another strange reason. In any case, I went through this dndcontrol file-sharing object that works like a charm for our purpose, controlling the transmitted data directly on the java side.

I got inspiration from this to write my own matlab proxy on top of java.awt.dnd.DropTarget , which is more general and closer to its java implementation node (i.e. it works just like a DropTarget java DropTarget , except that all data types have been converted to more standard and convenient Matlab types).

You can download my implementation from here:

And here is an example of use in order to do what you need (omit the Matlab axis from the explorer file):

 % % PURPOSE: % % Show how to add drop support from file explorer to some matlab axis % % SYNTAX: % % [] = DropListenerDemo(); % % USAGE: % % Simply drop files from file explorer into displayed axis. % %% function [] = DropListenerDemo() %[ % Create a figure with some axis inside fig = figure(666); clf; axes('Parent', fig); % Get back the java component associated to the axis % NB1: See ยง3.7.2 of Undocumented Secrets of Matlab Java Programming % NB2: or use findjobj, or javaObjectEDT for drop support onto other component types jFrame = get(handle(fig), 'JavaFrame'); jAxis = jFrame.getAxisComponent(); % Add listener for drop operations DropListener(jAxis, ... % The component to be observed 'DropFcn', @(s, e)onDrop(fig, s, e)); % Function to call on drop operation %] end function [] = onDrop(fig, listener, evtArg) %#ok<INUSL> %[ % Get back the dropped data data = evtArg.GetTransferableData(); % Is it transferable as a list of files if (data.IsTransferableAsFileList) % Do whatever you need with this list of files msg = sprintf('%s\n', data.TransferAsFileList{:}); msg = sprintf('Do whatever you need with:\n\n%s', msg); uiwait(msgbox(msg)); % Indicate to the source that drop has completed evtArg.DropComplete(true); elseif (data.IsTransferableAsString) % Not interested evtArg.DropComplete(false); else % Not interested evtArg.DropComplete(false); end %] end 

The object also supports capturing events DragEnter , DragOver , DropActionChanged , DragExit , so that you can customize all aspects of the drag operation. With little effort, it can also be expanded to support dragging and dropping images or dragging other types of data.

I hope you enjoy it and you find it common enough to think about other customs.

+1
source

There is a post in Matlab Central which uses a compiled java class. Fooobar.com/questions/1004722 / ... answer included code. With this solution, your demo might look like this:

 function demo % Set-up a figure droppable axis hFig = figure('name','DND example','numbertitle','off'); warning('off', 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame'); jFrame = get(hFig,'JavaFrame'); jAxis = jFrame.getAxisComponent; % dnccontrol class from above link dndcontrol.initJava(); dndcontrol(jAxis, @dropCallbackFcn); end function dropCallbackFcn(~, evt) fileparts(evt.Data{1}) % show dropped file path end 
-one
source

All Articles