-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanFolderAndMerge.m
134 lines (120 loc) · 5.02 KB
/
ScanFolderAndMerge.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
function BGCorrectedSpectrum = ScanFolderAndMerge(varargin)% (FolderName,SubfolderName,Wavelength,OverallBGFolder)
CurrentDir = pwd; % Remeber current directory
FolderName = varargin{1};
SubfolderName = varargin{2};
Wavelength = varargin{3};
if iscell(Wavelength) % Check if only wavelengtsh to consider
if length(Wavelength) == 2
WavelengthsToConsider = Wavelength{2};
Wavelength = Wavelength{1};
else
Acc = Wavelength{3};
WavelengthsToConsider = Wavelength{2};
Wavelength = Wavelength{1};
end
else
WavelengthsToConsider = [];
end
%% Move files if wrong
cd([FolderName,filesep,SubfolderName]);
Image1 = dir('*BackGroundWithLaser.mat');
Image2 = dir('*_Cell.mat');
if ~isempty(Image1)
if exist('PhaseImage') == 7 % if it is a folder
disp(['Error Phase Image does not exis in: ',FolderName,filesep,SubfolderName]);
else
movefile(Image1.name,'PhaseImages')
end
end
if ~isempty(Image2)
if exist('PhaseImage') == 7 % if it is a folder
disp(['Error Phase Image does not exis in: ',FolderName,filesep,SubfolderName]);
else
movefile(Image2.name,'PhaseImages')
end
end
cd([CurrentDir]);
%%
if length(varargin) == 4
OverallBGFolder = varargin{4}; % Get overall BG folder
cd([OverallBGFolder]);
if ~isempty(WavelengthsToConsider) % Consider only given Wavelengths
for q = 1:length(WavelengthsToConsider)
Backgrounds(q) = dir(['*Background',num2str(WavelengthsToConsider(q)),'*']);
end
else
Backgrounds = dir('*Background*'); % Get all BG names
end
cd([CurrentDir]);
else
cd([FolderName,filesep,SubfolderName]); %Jump into folder with measurements
BackgroundsApparent = dir('*Background*');
if ~isempty(WavelengthsToConsider) && ~isempty(BackgroundsApparent)
for q = 1:length(WavelengthsToConsider)
Backgrounds(q) = dir(['*Background',num2str(WavelengthsToConsider(q)),'*']);
end
else
Backgrounds = dir('*Background*'); % Get all BG names
end
OverallBGFolder = ([FolderName,filesep,SubfolderName]);
cd([CurrentDir]);
end
cd([FolderName,filesep,SubfolderName]);
if ~isempty(WavelengthsToConsider)
for q = 1:length(WavelengthsToConsider)
Signals(q) = dir(['*Signal',num2str(WavelengthsToConsider(q)),'*']);
end
else
Signals = dir('*Signal*'); % Get all Signal names
end
cd([CurrentDir]);
if ~(length(Signals) == length(Backgrounds)) && ~isempty(Backgrounds) % If not same length of BG and Signal there and BG acquired at all.
disp(['Not all Signals apparent check folder: ',FolderName,filesep,SubfolderName])
BGCorrectedSpectrum = [];
return;
end
for i = 1:length(Signals)
cd([FolderName,filesep,SubfolderName]);
SignalTemp = load(Signals(i).name); % Load signals ionto variable
cd([CurrentDir]);
SignalX{i} = (1./Wavelength - 1./SignalTemp.MatrixSignal(:,1))*10^7;
if ~isempty(Backgrounds)
cd([OverallBGFolder]);
if mod(i,length(Backgrounds)) == 0
CounterBG = length(Backgrounds);
else
CounterBG = mod(i,length(Backgrounds));
end
Background = load(Backgrounds(CounterBG).name); % load BG into variable(if existent) and repeat if more signals acquired (might be wrong)
cd([CurrentDir]);
[MatrixSignalCleaned,MatrixBGCleaned] = CleanCosmicRayV2(SignalTemp.MatrixSignal(:,2:end),Background.MatrixBG(:,2:end)); % Clean CosmicRay
if isnan(MatrixSignalCleaned)
waitfor(msgbox(['Please check the measurement in: ',10,[FolderName,filesep,SubfolderName],10, 'delete if corrupted']))
end
SignalY{i} = nanmedian(MatrixSignalCleaned,2)-nanmedian(MatrixBGCleaned,2); % Get Median of cleaned signal minus BG
else
[MatrixSignalCleaned] = CleanCosmicRayV2(SignalTemp.MatrixSignal(:,2:end)); % Clean Cosmic Rays.
%% Test for 1 measurement
if exist('Acc')
SignalY1 = nanmedian(MatrixSignalCleaned(SignalX{i}<2050,:),2); % Get median of cleaned signal
SignalY3 = nanmedian(MatrixSignalCleaned(SignalX{i}>2150,:),2); % Get median of cleaned signal
SignalY2 = nansum(MatrixSignalCleaned(SignalX{i}>=2050 & SignalX{i} <=2150,:),2)/Acc; % Get median of cleaned signal
SignalY{i} = [SignalY1;SignalY2;SignalY3];
else
%%
SignalY{i} = nanmedian(MatrixSignalCleaned,2); % Get median of cleaned signal
end
end
end
[SignalX,SignalY] = OrderSpectra(SignalX,SignalY); % Order if for any reason not in order
Spectrum = StitchTogether(SignalX,SignalY); % STitch spectra together to make one large spectrum out of it for BG correction
%% Just a trY !!!!!!
Spectrum(:,2) = smooth(Spectrum(:,2)); % Test if smoothing is good.
if max(Spectrum(Spectrum(:,1)>1061 & Spectrum(:,1)<1065,2)) < 0
%BGCorrectedSpectrum = [];
BGCorrectedSpectrum = CorrectFluorescentBG(Spectrum(:,1),Spectrum(:,2));
else
BGCorrectedSpectrum = CorrectFluorescentBG(Spectrum(:,1),Spectrum(:,2));
end
cd(CurrentDir); % Jump back to directory at start
end