-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlistresortfiles.m
101 lines (80 loc) · 3.73 KB
/
listresortfiles.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%list re-sorted files
% listing subjects (NB: in xls file, subjects are now pooled together)
Subjects={'Rigel','Sixx','Hilda'};
AllResortDir='Y:\Team Cerebellum\Resorted Files\';
%% Set preferences with setdbprefs.
s.DataReturnFormat = 'cellarray';
s.ErrorHandling = 'store';
s.NullNumberRead = 'NaN';
s.NullNumberWrite = 'NaN';
s.NullStringRead = 'null';
s.NullStringWrite = 'null';
s.JDBCDataSourceFile = '';
s.UseRegistryForSources = 'yes';
s.TempDirForRegistryOutput = ['C:\Users\' getenv('username') '\AppData\Local\Temp'];
s.DefaultRowPreFetch = '10000';
setdbprefs(s)
% Make connection to database. Note that the password has been omitted.
% Using ODBC driver.
conn = database('Excel Files','','password');
%% write file names to xls file
for dirs=1:length(Subjects)
%get file names
ResortDirInfo=dir([AllResortDir Subjects{dirs}]);
FileNames={ResortDirInfo.name};
if ~isempty(find(~cellfun('isempty',regexp(FileNames,'^\d','match')), 1))
disp('File(s) need Initial letter')
dirs
return;
end
FileNames=FileNames(~cellfun('isempty',strfind(FileNames,'smr')) | ~cellfun('isempty',strfind(FileNames,'SMR')));
FileNames=regexprep(FileNames, '(.SMR$)|(.smr$)','')';
%Look filenames in the list
% Read filenames from xls file.
e = exec(conn,['SELECT ALL Filename FROM "allfiles$"']); % previously Subjects{dirs} instead of allfiles, when files where split in three sheets
e = fetch(e);
close(e)
% Assign data to output variable.
filelist = e.Data(:,1);
%lookup which files are missing from the list
addfiles=FileNames(~ismember(FileNames,filelist));
if ~isempty(addfiles)
% Write data to database.
insert(conn,['"allfiles$"'],{'Filename'},addfiles)
end
%check segmentation. Write crude segmentation if missing
% set depth limits for top cortex / dentate / bottom cortex
% Read filenames from xls file.
e = exec(conn,['SELECT ALL Filename,"Presumed location (depth based)" FROM "allfiles$"']); % previously Subjects{dirs} instead of allfiles, when files where split in three sheets
e = fetch(e);
close(e)
% Assign data to output variable
filelist = e.Data(:,1); %updated filelist
presumeddepth = e.Data(:,2);
compart=cell(size(filelist,1),1);
if strcmp('Rigel',Subjects{dirs})
cdn_depth=19000;
bcx_depth=22000;
elseif strcmp('Sixx',Subjects{dirs})
cdn_depth=11000;
bcx_depth=17000;
elseif strcmp('Hilda',Subjects{dirs})
cdn_depth=19000;
bcx_depth=26000;
end
%get depth
recdepth=regexp(filelist,'_\d\d+','match');
recdepth=cellfun(@(x) regexp(x{:},'\d+','match'), recdepth,'UniformOutput',false);
compart(cellfun(@(x) str2double(x)<cdn_depth, recdepth) & ismember(filelist,FileNames))={'top_cortex'};
compart(cellfun(@(x) (str2double(x)>=cdn_depth && str2double(x)<=bcx_depth), recdepth) & ismember(filelist,FileNames))={'dentate'};
compart(cellfun(@(x) str2double(x)>bcx_depth, recdepth) & ismember(filelist,FileNames))={'bottom_cortex'};
fileidx=cellfun(@(x) strfind(filelist,x),FileNames,'UniformOutput', false);
fileptr=cell2mat(cellfun(@(x) find(~cellfun('isempty',x),1), fileidx,'UniformOutput', false));
compart=compart(fileptr);
% Write depth to xls file.
update(conn,'"allfiles$"',{'"Presumed location (depth based)"'},compart,cellfun(@(x) ['where Filename = ' '''' x ''''],FileNames, 'UniformOutput', false));
...['WHERE ID BETWEEN ' num2str(find(ismember(filelist,FileNames),1)) ' AND ' num2str(find(ismember(filelist,FileNames),1,'last'))])
% ['where Filename =' FileNames]) % ismember(filelist,FileNames)
end
%% Close database connection.
close(conn)