-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistFiles.m
59 lines (54 loc) · 1.7 KB
/
listFiles.m
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
function [ names ] = listFiles(wildcard)
% LISTFILES List files matching a wildcard
%
% ## Syntax
% names = listFiles(wildcard)
%
% ## Description
% names = listFiles(wildcard)
% Return file names matching the wildcard as a cell array.
%
% ## Input Arguments
%
% wildcard -- Input filename wildcard
% A wildcard expression for `ls()`, containing the path, if the desired
% files are located in a different directory from the current directory.
% (i.e. The wildcard is used as-is - This function does not add any
% contextual information.)
%
% ## Output Arguments
%
% names -- Filepaths and names
% A cell vector, where each element is a character containing the name
% and path of a file in the output of `ls()`.
%
% ## Notes
% - This function treats Windows systems as a special case, and assumes
% that `ls()` has Linux-specific behaviour on all other systems.
%
% See also ls
%
% Bernard Llanos
% University of Alberta, Department of Computing Science
% File created January 23, 2018 for a research project under Dr. Y.-H. Yang
% Revised January 20, 2019 for the course CMPUT 428/615
nargoutchk(1,1)
narginchk(1,1)
if ispc
filepath = fileparts(wildcard);
raw_names = ls(wildcard);
names = cell(size(raw_names, 1), 1);
for i = 1:size(raw_names, 1)
names{i} = fullfile(filepath, strtrim(raw_names(i, :)));
end
else
names = strtrim(strsplit(ls(wildcard), {' ','\f','\n','\r','\t','\v'}));
names = names(1:(end - 1)); % There is always a terminating newline
% Filepaths that contain spaces will be padded with single quotes
for i = 1:length(names)
if names{i}(1) == '''' && names{i}(end) == ''''
names{i} = names{i}(2:(end - 1));
end
end
end
end