The uiFileDnD implementation allows to set up a callback fired when files and/or folders are dropped onto a (ui)figure component.
In the callback, full file/folder names are captured for user to decide the action. dropXY, Ctrl and Shift key status during the drop event are also reported.
Example to drop file/folder onto uilistbox of uifigure:
obj = uilistbox(uifigure, 'Position', [80 100 400 100]);
uiFileDnD(obj, @(o,dat)set(o,'Items',dat.names));
Example to drop file/folder onto listbox of figure:
obj = uicontrol(figure, 'Style', 'listbox', 'Position', [80 100 400 100]);
uiFileDnD(obj, @(o,dat)set(o,'String',dat.names));
Note:
-
File DnD onto uifigure works since Matlab R2020b (since R2025a for Linux).
-
The following line needs to be added into startup.m file for R2025a and likely later:
try addprop(groot, 'ForceIndependentlyHostedFigures'); catch, end
-
java_dnd.m & MLDropTarget.class are for figure() before R2025a, and can be removed in the future.