-
Notifications
You must be signed in to change notification settings - Fork 3
/
prepSessid.m
executable file
·101 lines (72 loc) · 2.28 KB
/
prepSessid.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
function newidlist = prepSessid(idfile,opstr,idlist)
% newidlist = prepSessid(idfile,opstr,idlist)
% prepare sessid file for the study
% idfile: text file which store the ID, every row is one ID
% opstr: operation string: 'make','add','del'
% idlist: IDs to be preprocessed(a Nx1 string cell array)
% % prepare sessid
% idfile = fullfile(datadir,'sessid.txt');
% % idlist = {'S0250','S0332','S0029'};
% % newidlist = prepsessid(idfile,'add',idlist);
switch lower(opstr)
case 'make'
disp('Make ID File')
newidlist = makeID(idfile,idlist);
case 'add'
disp('Add ID to ID File')
newidlist = addID(idfile,idlist);
case 'del'
disp('Del ID from ID File')
newidlist = delID(idfile,idlist);
otherwise
disp('Unknown Opstr.')
end
function newidlist = makeID(idfile,idlist)
% Make idfile according the idlist
% idfile: name for sessid file which store the ID, every row is one ID
% idlist: ID to be deletd(a string cell array)
newidlist = idlist;
fid = fopen(idfile,'w+');
for i = 1:length(newidlist)
fprintf(fid,'%s\n',newidlist{i})
end
fclose(fid);
function newidlist = addID(idfile,idlist)
% Add idlist to the idfile
% idfile: text file which store the ID, every row is one ID
% idlist: ID to be added(a string cell array)
fid = fopen(idfile);
newidlist = textscan(fid,'%s');
newidlist = newidlist{1};
fclose(fid);
if size(idlist,1)==1
idlist = idlist';
end
fid = fopen(idfile,'a+');
for i = 1:length(idlist)
fprintf(fid,'%s\n',idlist{i});
end
fclose(fid);
newidlist = [newidlist;idlist];
function newidlist = delID(idfile,idlist)
% Delete id in the idlist from the idfile
% idfile: text file which store the ID, every row is one ID
% idlist: ID to be deletd(a string cell array)
fid = fopen(idfile);
newidlist = textscan(fid,'%s');
newidlist = newidlist{1};
fclose(fid);
idx = zeros(length(newidlist),1);
for i = 1:length(idlist)
for j = 1:length(newidlist)
if strcmp(newidlist{j}, idlist{i})
idx(j) = 1;
end
end
end
newidlist = newidlist(idx==0);
fid = fopen(idfile,'w+');
for i = 1:length(newidlist)
fprintf(fid,'%s\n',newidlist{i})
end
fclose(fid);