-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesCellArr.m
82 lines (69 loc) · 2.58 KB
/
filesCellArr.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
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
function fileNameCellArr = filesCellArr( Dir, nameFrag, ext, toSaveToDisk, warnWindoze )
% function fileNameCellArr = filesCellArr( Dir, nameFrag, ext, toSaveToDisk )
% Create a cell array of strings with all the files with extention 'ext'
% (not including the dot) and whose name contains string nameFrag
% to be found in directory Dir. Similar to listFiles, but strips end empty
% chars etc.
try, Dir; catch Dir = pwd; end;
try, nameFrag; catch nameFrag=''; end;
try, ext; catch ext = 'mat'; end;
try, toSaveToDisk; catch toSaveToDisk=0; end;
try, warnWindoze; catch warnWindoze = 0; end;
% preliminaries
fCArr = {};
extLen = length(ext);
wildName = ['*' nameFrag '*'];
if ~isempty(ext); wildName = [wildName '.' ext]; end;
cwd = pwd;
% Move to the dir. in question and list the relevant files:
cd(Dir);
thisComputer = computer;
if ~isempty(strfind(thisComputer,'WIN')) || ~isempty(strfind(thisComputer,'redhat-linux'))
% We are in Windoze or redhat - for some bizarre reason it seems
% to work OK in CentOS redhat ... ?!
try
fStrMat = ls(wildName);
catch
fileNameCellArr = {};
warning(['No ' wildName ' files found ...']);
return;
end
if warnWindoze; warning('filesCellArr may be operating in Windows'); end;
elseif ~isempty(strfind(thisComputer,'GLNX')) || ~isempty(strfind(thisComputer,'linux-gnu'))
% we are in matlab Linux or in octave running e.g. in a VM ubuntu 16 ... :-(
try
fStrMat = ls_ms( ls(wildName,'--format','single-column') );
catch
fStrMat = ls(wildName,'--format','single-column'); % for matlab running in CentOS and such like ...
end
if length(fStrMat) < 1 % ie other failure of ls / ls_ms commands ... :-(
fileNameCellArr = {};
warning(['No ' wildName ' files found ...']);
return;
end
else
error('not ready for this ''computer'' ');
end
% Now strip any trailing white spaces from the file names
for i=1:size(fStrMat, 1)
fCArr{i} = fStrMat(i,:);
if ~isempty(ext) % actively look for the ext string
while ~strcmp(fCArr{i}(end-extLen+1:end),ext)
fCArr{i} = fCArr{i}(1:end-1);
end
else % just strip ' ' characters, if any:
while length(fCArr{i}) > 0 && strcmp(fCArr{i}(end),' ')
if length(fCArr{i}) > 1
fCArr{i} = fCArr{i}(1:end-1);
else % there is only one element, and that is a blank :-(
fCArr{i} = [];
end
end
end
end
fileNameCellArr = fCArr;
cd(cwd);
if toSaveToDisk
save('lsCellArr.mat','fileNameCellArr');
end
end % of whole function