-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseImageFiles.m
140 lines (106 loc) · 4.01 KB
/
parseImageFiles.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
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
128
129
130
131
132
133
134
135
136
137
138
139
function [imageID] = parseImageFiles(imFileNames,chanNum)
%PARSEIMAGEFILES Summary of this function goes here
% Detailed explanation goes here
% Ignore mismatches; throw them out and throw out warning message
% Check if number of inputs are correct
if nargin < 2 || nargin > 2
error('Must input two arguments');
end
% Check if Channel number input is numeric and 3 or less
if ~isnumeric(chanNum)
error('Second input argument must be numeric')
elseif chanNum > 3
error('Second input cannot be higher than 3')
end
% If one channel
% Look for number and condition separated by '_'
if chanNum == 1
nisslCond = '';
roiCond = '';
imIter = 1;
nToggle = 1; rToggle = 1;
while (nToggle || rToggle || imIter < length(imFileNames))
str = imFileNames{imIter};
[~,~,e] = fileparts(str);
foundNames = strsplit(str(1:strfind(str,e)-1),'_');
if ismember(nisslCond,foundNames) || ismember(roiCond,foundNames)
imIter = imIter + 1;
continue
end
cond1Sl = listdlg('PromptString','Select Condition Name','SelectionMode','Single',...
'ListString',foundNames);
condSel = foundNames{cond1Sl};
cQuest = sprintf('Is %s the Nissl or ROI condition?',condSel);
cond1Q = questdlg(cQuest, 'Condition', 'Nissl', 'ROI', 'Nissl');
switch cond1Q
case 'Nissl'
nisslCond = condSel;
nToggle = 0;
case 'ROI'
roiCond = condSel;
rToggle = 0;
end
imIter = imIter + 1;
end
% Get list of both condition files
conTypes = {'Nissl', 'ROI'};
conds = {nisslCond , roiCond};
nisslFiles = cell(length(imFileNames),1);
roiFiles = cell(length(imFileNames),1);
for ci = 1:2
condCheck = conds{ci};
imCount = 1;
for imI = 1:length(imFileNames)
if strcmp(regexp(imFileNames{imI},condCheck,'match'),condCheck);
switch conTypes{ci}
case 'Nissl'
nisslFiles{imCount} = imFileNames{imI};
imCount = imCount + 1;
case 'ROI'
roiFiles{imCount} = imFileNames{imI};
imCount = imCount + 1;
end
else
continue
end
end
end
% Clean up
nisslFiles = nisslFiles(cellfun(@(x) ~isempty(x), nisslFiles));
roiFiles = roiFiles(cellfun(@(x) ~isempty(x), roiFiles));
% Extract Numbers
combinedFiles = {nisslFiles , roiFiles};
nisslFnums = cell(length(imFileNames),1);
roiFnums = cell(length(imFileNames),1);
for conI = 1:2
tempFnames = combinedFiles{conI};
for fileI = 1:length(tempFnames)
switch conTypes{conI}
case 'Nissl'
nisslFnums{fileI} = str2double(regexp(tempFnames{fileI},'[0-9]','match'));
case 'ROI'
roiFnums{fileI} = str2double(regexp(tempFnames{fileI},'[0-9]','match'));
end
end
end
% Clean up
nisslFnums = nisslFnums(cellfun(@(x) ~isempty(x), nisslFnums));
roiFnums = roiFnums(cellfun(@(x) ~isempty(x), roiFnums));
% Sort and save into struct for output
[~,nisslOrder] = sort(cell2mat(nisslFnums));
[~,imageOrder] = sort(cell2mat(roiFnums));
imageID.Nissl.Nums = nisslFnums(nisslOrder);
imageID.Nissl.FNames = nisslFiles(nisslOrder);
imageID.ROI.Nums = roiFnums(imageOrder);
imageID.ROI.FNames = roiFiles(imageOrder);
% If more than one channels
% Look for number
elseif chanNum > 1
imFnums = cellfun(@(x) str2double(regexp(x,'[0-9]+','match')), imFileNames);
imFnums = num2cell(imFnums);
% Sort and save into struct for output
[~,imageOrder] = sort(cell2mat(imFnums));
imageID.Image.Nums = imFnums(imageOrder);
imageID.Image.FNames = imFileNames(imageOrder);
end
end