-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude.m
More file actions
127 lines (94 loc) · 3.92 KB
/
include.m
File metadata and controls
127 lines (94 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
%% Include.m
% include folders in the path
% Apache V2 License - Copyright (c) 2020 Amin Yahyaabadi - aminyahyaabadi74@gmail.com
% https://github.com/aminya/Dispatch.m
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function include(method, varargin)
% include function
%
% # Arguments:
% `include(method::String, [folders::Array{String}/Cell{Char}])`
%
% - method: can be `"all","all_exclude","specific","GUI_all","GUI_specific"`
% - folders (optional):
% - if method is `"all_exclude"`: pass the folder names that should be excluded
% - if method is `"specific"`: pass the folder names that should be included
%
% Folders specified in the 2nd argument can have a relative as well as absolute path.
%
% # Example run_include
% Choose the method, and run the function.
% ```matlab
% include("all");
% ```
%
% Pass a 2nd output to include/exclude specific folders if you chose "all_exclude" or "specificFolders"
% ```matlab
% include("specific", ["src", "examples"]); % or include("specific", {'src', 'examples'});
% ```
switch method
%% To include all
case "all"
addpath(pwd); % add root folder
% add subfolders
folders=dirFolder(pwd);
for i=1:length(folders)
addpath(genpath(folders{i}))
end
disp("Folders of current working and their subfolders are added to the path");
%% To include all excluding some folders
case "all_exclude"
% folders to be excluded
excludedFolders = varargin{1};
addpath(pwd); % add root folder
folders=dirFolder(pwd); % all folders
% folders to be included
includedFolders=folders(~ismember(folders,excludedFolders));
for i=1:length(includedFolders)
addpath(genpath(includedFolders{i}))
end
disp("Folders of current working and their subfolders excluding those specified are added to the path");
%% To include specific folders
case "specific"
includedFolders = varargin{1};
addpath(pwd); % add root folder
for i=1:length(includedFolders)
addpath(genpath(includedFolders{i}))
end
disp("Sepecified folders and their subfolders are added to the path");
%% To include all under a folder with GUI
case "GUI_all"
rootDir = uigetdir(pwd, 'Select a folder');
addpath(rootDir); % add root folder
% add subfolders
folders=dirFolder(rootDir);
for i=1:length(folders)
addpath(genpath(folders{i}))
end
disp("selected folders and their subfolders are added to the path");
%% To include specific folders with GUI
case "GUI_specific"
% press cancel if you are finished with adding
rootDir=1;
while 1 % continue until user press cancel
rootDir = uigetdir(pwd, 'Select a folder');
if rootDir==0
break;
else
addpath(genpath(rootDir));
end
end
disp("Selected folders and their subfolders are added to the path");
end
end
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [folders] = dirFolder(parentDir)
% get folder names in a directory
% modified from https://www.mathworks.com/matlabcentral/answers/166629-is-there-any-way-to-list-all-folders-only-in-the-level-directly-below-a-selected-directory#comment_624696
all = dir(parentDir); %list of all files and folders
names = {all.name};
% seperating folders indeces
idxFolder = [all.isdir] & ~strcmp(names, '.') & ~strcmp(names, '..');
% Extract only those that are directories.
folders = names(idxFolder);
end