📄 folderdlg.m
字号:
function answer = folderdlg(varargin)% folderdlg Application M-file for folderdlg.fig% answer = folderdlg return the answer.% folderdlg('callback_name') invoke the named callback.% folderdlg([left bottom]) locates the dialog.% Last Modified by GUIDE v2.5 15-Jan-2004 08:37:30error(nargchk(0,4,nargin)) % function takes only 0 or 4 argumentif nargin == 0 | isnumeric(varargin{1}) % LAUNCH GUI fig = openfig(mfilename,'reuse'); % Generate a structure of handles to pass to callbacks, and store it. handles = guihandles(fig); % determine the number of fixed disc drives (C:\, D:\, ...) drives = []; for(i = 1:26) if(exist(['B'+i ':\']) == 7) drives = [drives i]; % 'C:\' -> 1, 'D:\' -> 2, ... end end handles.lastdrive = drives(end); % store last drive % set current drive currpath = pwd; handles.currentdrive = find(drives == currpath(1)-'A'-1); % find drive letter in 'drives' set(handles.Drivepopupmenu, 'Value', handles.currentdrive); % store handle as guidata guidata(fig, handles); % set choice for popup menu existdrives = cell(length(drives),1); for(i = 1:length(existdrives)) existdrives(i) = {['B'+drives(i) ':\']}; end set(handles.Drivepopupmenu, 'String', existdrives) % Position figure if nargin == 1 pos_size = get(fig,'Position'); pos = varargin{1}; if length(pos) ~= 2 error('Input argument must be a 2-element vector') end new_pos = [pos(1) pos(2) pos_size(3) pos_size(4)]; set(fig,'Position',new_pos,'Visible','on') figure(fig) end % Populate the listbox (current path) load_listbox(currpath,handles) % Wait for callbacks to run and window to be dismissed: uiwait(fig); % UIWAIT might have returned because the window was deleted using % the close box - in that case, return 'cancel' as the answer, and % don't bother deleting the window! if ~ishandle(fig) answer = 'cancel'; else % valid selection made... % Also, we need to delete the window. % retrieve the latest copy of the 'handles' struct, and return the answer. handles = guidata(fig); answer = handles.answer; delete(fig); end elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK % set current drive currpath = pwd; handles.currentdrive = currpath(1) - 'B'; % 'C:\' -> 1, 'D:\' -> 2, 'E:\' -> 3, ... try if (nargout) [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard else feval(varargin{:}); % FEVAL switchyard end catch disp(lasterr); endend% --------------------------------------------------------------------function varargout = Cancelpushbutton_Callback(h, eventdata, handles, varargin)handles.answer = 'cancel';guidata(h, handles);uiresume(handles.figure1);% --------------------------------------------------------------------function varargout = OKpushbutton_Callback(h, eventdata, handles, varargin)handles.answer = pwd;guidata(h,handles);uiresume(handles.figure1);% --------------------------------------------------------------------function varargout = Drivepopupmenu_Callback(h, eventdata, handles, varargin)savecurrpath = pwd;savecurrdrive = handles.currentdrive;handles.currentdrive = get(handles.Drivepopupmenu, 'Value'); % set new current drivetry kk = get(handles.Drivepopupmenu, 'String'); cd(kk{handles.currentdrive}); % change drive currpath = pwd; % get new pathcatch beep; disp('Drive not accessible'); handles.currentdrive = savecurrdrive; % couldn't change drive -> back to previous set(handles.Drivepopupmenu, 'Value', handles.currentdrive); currpath = savecurrpath;end% update listboxload_listbox(currpath,handles)% --------------------------------------------------------------------function varargout = folderlistbox_Callback(h, eventdata, handles, varargin)if strcmp(get(handles.figure1,'SelectionType'),'open') index_selected = get(handles.folderlistbox,'Value'); file_list = get(handles.folderlistbox,'String'); filename = file_list{index_selected}; cd (filename) load_listbox(pwd,handles)end% ------------------------------------------------------------% Read the current directory and sort the names% ------------------------------------------------------------function load_listbox(dir_path,handles)cd (dir_path)dir_struct = dir(dir_path);all_names = {dir_struct.name}';dir_names = all_names(find([dir_struct.isdir]));[sorted_names,sorted_index] = sortrows(dir_names);handles.dir_names = [sorted_names];handles.sorted_index = [sorted_index];guidata(handles.figure1,handles)set(handles.folderlistbox,'String',handles.dir_names,... 'Value',1)set(handles.text1,'String',pwd)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -