forked from ilkkavir/BAFIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
separate_mat.m
93 lines (83 loc) · 2.3 KB
/
separate_mat.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
function success = separate_mat(mergedfile,delfile)
%
% success = separate_mat(mergedfile,delfile)
%
% Separate data in a merged file into separate matlab files
%
% INPUT:
% mergedfile the merged output file or a directory containing one or more merged files
% delfile true to delete the merged file after separating the files
%
% OUTPUT:
% success 0 if the process was successful, otherwise see error codes below
%
% Error codes
% 0 success
% 1 failed to read fieldnames from the input file
% 2 failed to load data from the input file
% 3 failed to save a mat file
% 4 failed to delete the input file
% 5 the input file is not a merged GUISDAP output file
%
% IV 2022
%
success = 0;
% if a directory was given as input
if exist(mergedfile,'dir')
% list matlab files in directory
matfiles = dir(fullfile(mergedfile,'*.mat'));
for k=1:length(matfiles)
disp(matfiles(k).name)
success(k) = separate_mat(fullfile(mergedfile,matfiles(k).name),delfile);
end
return
end
% the output directory
dirparts = strsplit(mergedfile,filesep);
if length(dirparts)==1
odir = pwd;
else
odir = fileparts(mergedfile);
end
% field names in the merged file
try
fnames = sort(fieldnames(matfile(mergedfile)));
catch
success = 1;
return
end
ismergedfile = false;
for k=1:length(fnames)
fname = char(fnames(k));
if length(fname)==12
if fname(1:4)=='data'
% load contents of one separate mat file
try
tmpdata = getfield(load(mergedfile,fname),fname);
catch
success = 2;
return
end
outfile = fullfile(fileparts(mergedfile),[fname(5:12) '.mat']);
try
save(outfile,'-struct','tmpdata');
catch
success = 3;
return
end
ismergedfile = true;
disp(outfile)
end
end
end
if delfile & ismergedfile
try
delete(mergedfile)
catch
success = 4;
end
end
if ~ismergedfile
success = 5;
end
end