一般来说,MATLAB自身的GUI是不能够实现将文件夹或者文字拖拽进去进行读取的,就像用QQ邮箱上传文件一样:
然后我们用MATLAB实现的效果如下:
这个控件并不是用过MATLAB自带的函数实现的,而是通过调用Java来实现,先要定义好Java的对象,然后在MATLAB程序中进行调用,使用起来我们并不需要知道其中的原理,只需要当做黑匣子就可以了。
一定不要嫌麻烦,我会给出文件夹下载可以直接使用,你调整一下代码弄成自己的就好了,下面这个文件保存为dndcontrol.m
classdef (CaseInsensitiveProperties) dndcontrol < handle
%DNDCONTROL Class for Drag & Drop functionality.
% obj = DNDCONTROL(javaobj) creates a dndcontrol object for the specified
% Java object, such as 'javax.swing.JTextArea' or 'javax.swing.JList'. Two
% callback functions are available: obj.DropFileFcn and obj.DropStringFcn,
% that listen to drop actions of respectively system files or plain text.
%
% The Drag & Drop control class relies on a Java class that need to be
% visible on the Java classpath. To initialize, call the static method
% dndcontrol.initJava(). The Java class can be adjusted and recompiled if
% desired.
%
% DNDCONTROL Properties:
% Parent - The associated Java object.
% DropFileFcn - Callback function for system files.
% DropStringFcn - Callback function for plain text.
%
% DNDCONTROL Methods:
% dndcontrol - Constructs the DNDCONTROL object.
%
% DNDCONTROL Static Methods:
% defaultDropFcn - Default callback function for drop events.
% demo - Runs the demonstration script.
% initJava - Initializes the Java class.
% isInitialized - Checks if the Java class is visible.
%
% A demonstration is available from the static method dndcontrol.demo().
%
% Example:
% dndcontrol.initJava();
% dndcontrol.demo();
%
% See also:
% uicontrol, javaObjectEDT.
%
% Written by: Maarten van der Seijs, 2015.
% Version: 1.0, 13 October 2015.
properties (Hidden)
dropTarget;
end
properties (Dependent)
%PARENT The associated Java object.
Parent;
end
properties
%DROPFILEFCN Callback function executed upon dropping of system files.
DropFileFcn;
%DROPSTRINGFCN Callback function executed upon dropping of plain text.
DropStringFcn;
end
methods (Static)
function initJava()
%INITJAVA Initializes the required Java class.
%Add java folder to javaclasspath if necessary
if ~dndcontrol.isInitialized();
classpath = fileparts(mfilename('fullpath'));
javaclassp