-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsf_organizer.m
287 lines (221 loc) · 8.13 KB
/
sf_organizer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
function out = sf_organizer (stim_idx,savepath,varargin)
%% About
%This function is the main save function for all data in the app. It creates
%a new folder for a given stimulus if a folder does not yet exist or uses
%an existing folder in case a folder for that stimulus does exist. It save
%variables to given folder or subfolder and keeps track of all variables
%and subfolders created by creating a stimulus Data file which will be
%refered to in the Stimulus_Info file in the main analysis .mat file.
%The return of this function is the path of the folder, subfolder, or in
%case a variable was saved the whole path to the variable.
%@MSeifert 2020
%% Input control
defaultOverwrite = false;
default_update_data = true;
defaultCollect = false;
p = inputParser;
validScalarPosNum = @(x) isnumeric(x) && isscalar(x);
%validMatrixShape = @(x) isnumeric(x) && (size(x,1) ==1) && (size(x,2) == 2);
addRequired(p,'stim_idx',validScalarPosNum);
addRequired(p,'savepath',@ischar);
addParameter(p,'subfoldername',@ischar);
addParameter(p,'pathname',@ischar);
addParameter(p,'variable_name',@isstring);
addParameter(p,'variable',@(x)validateattributes(x,{'nonempty'}));
addParameter(p,'filename',@isstring);
addParameter(p,'overwrite',defaultOverwrite,@islogical);
addParameter(p,'update_data',default_update_data,@islogical);
addParameter(p,'collect_files',defaultCollect,@islogical);
parse(p,stim_idx,savepath,varargin{:});
%Just for debugging
% out = p;
%% Set variables
%This will remove all variables from the input structure which are not
%beeing used
fields = fieldnames(p.Results);
variables = p.Results;
Data_idx = 1;
for ii = 1:length(fields)
if isa(variables.(fields{ii}),'function_handle')
variables = rmfield(variables,fields{ii});
end
end
out = variables;
% Add some global variables
stim_folder_name = ['Stimulus_',num2str(stim_idx)];
datafile_name = ['Stimulus_',num2str(variables.stim_idx),'_DataFile.mat'];
%Check if this is run in parallel
%Parallel access to a matfile is not possible
% answer = is_in_parallel();
%
% if answer == 1 && update_data == 1
% error("Cant update stimulus data in parallel pool")
% end
%% Check pathname
%If no pathname is given, current folder is set to be the savepath
if ~isfield(variables,'pathname')
try
S = load(savepath,'pathname');
pathname = S.pathname;
clear S
catch
error('No pathname found in savefile, savefile incorrect');
out = 0;
end
else
%In case pathname is given as input
pathname = variables.pathname;
end
cd(pathname)
%% Check if stimulus folder exists in pathname
%First we check if the pathname is the stimulus folder (might be a mistake
%people can do
if strcmp(pathname,stim_folder_name)
out = 0;
error('Pathname set to stimulus folder name, check pathname input')
end
%Next, check if the folder does already exist
c_data_path = strcat(pathname, stim_folder_name,"\",datafile_name);
if isfolder(stim_folder_name)
%disp('Stimulus folder does exist, no new folder created');
cd(stim_folder_name)
if variables.update_data == 1
M = matfile(c_data_path,'Writable',true);
Data = M.Data;
end
out = pwd;
else
%Otherwise the folder will be created newly
mkdir(stim_folder_name);
cd(stim_folder_name);
%If the folder didnt exist before, than also a new data file has to be
%created to keep track of the subfolders and subfiles
if variables.update_data == 1
M = matfile(c_data_path,'Writable',true);
end
%Write the location of the new create matfile into the main analysis
%file of the app
try
S = load(savepath,'Stimulus_info');
Stimulus_info = S.Stimulus_info;
catch
Stimulus_info = {};
end
Stimulus_info{stim_idx} = [pathname,stim_folder_name,'\',datafile_name];
S = matfile(savepath,'Writable',true);
S.Stimulus_info = Stimulus_info;
%Create empty datafile structure
Data(1).Folder = "Main";
Data(1).Files = {};
M.Data = Data;
Data_idx = 1;
out = pwd;
end
%% Create subfolder or switch folder
if isfield(variables, 'subfoldername')
%Check if subfolder exists in stimulus folder
if isfolder(variables.subfoldername)
cd(variables.subfoldername)
% variables.subfoldername
if variables.update_data == 1
Data_idx = find([Data.Folder] == variables.subfoldername);
end
sf = 1; %This keeps the information that we are now in the subfolder dir
out = pwd;
else
mkdir(variables.subfoldername)
cd(variables.subfoldername)
if variables.update_data == 1
Data_length = length(Data);
Data(Data_length+1).Folder = variables.subfoldername;
Data_idx = Data_length+1;
end
out = pwd;
sf = 1;
end
end
%% Save variable
%This only applies if a variable and a name for that variable is given in
%the inputs otherwise skip this part
%Check if variable is figure, which requires a different way to save it
if isfield(variables,'variable_name')&&isfield(variables,'variable')
try
figure_save = isfigure(variables.variable);
catch ME
figure_save = false;
warning([ME.message, ' Ignore, if no error thrown.'])
end
if figure_save
file_ending = '.fig';
else
file_ending = '.mat';
end
%Creat new sturcture to dynamically save the variable
save_struc.(variables.variable_name) = variables.variable;
%Check if the a filename was given
if ~isfield(variables,'filename')
filename = variables.variable_name;
else
filename = variables.filename;
end
%First check if the variable already exists
if isfile([filename,file_ending])&& variables.overwrite == false
% Include the desired Default answer
opts.Interpreter = 'tex';
opts.Default = 'No';
quest = ['Do you want to overwrite the existing file: "', filename,file_ending];
answer = questdlg(quest,'Boundary Condition','Yes','No',opts);
if strcmp(answer,'No')
return
else
if figure_save
savefig(variables.variable,filename);
else
save(filename,'-struct','save_struc','-v7.3');
end
if variables.update_data == 1
Data(Data_idx).Files{end+1} = [filename,file_ending];
end
out = [pwd,'\',filename,file_ending];
end
else
if figure_save
savefig(variables.variable,filename);
else
save(filename,'-struct','save_struc');
end
if variables.update_data == 1
Data(Data_idx).Files{end+1} = [char(filename),file_ending];
end
out = [pwd,'\',filename,file_ending];
end
end
if variables.update_data == 1
for i = 1:length(Data)
if numel(Data(i).Files) > 1 %Because unique doesnt work (and isnt required)
%if there is only one file in the datafile
Data(i).Files = unique(Data(i).Files);
end
end
M.Data = Data;
end
if variables.collect_files == 1 && variables.update_data == 1
M = matfile([pathname,'\',stim_folder_name,'\',datafile_name],'Writable',true);
Data = M.Data;
files = dir;
files = {(files(3:end,:).name)};
%Sort files depending on ending numbers
number_end = regexp(files,'\d*','Match');
try
if ~any(cellfun(@isempty,number_end))
%Only sort if all files contain numbers, otherwise dont sort
[~,Isort] = sort(str2double([number_end{:}]));
files = files(Isort);
end
catch
end
Data(Data_idx).Files = files;
M.Data = Data;
end
out = string(out);
end