-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_help.m
132 lines (128 loc) · 3.23 KB
/
pspm_help.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
function [information, arguments] = pspm_help(func_name)
% ● Description
% pspm_help returns the description and arguments of
% a specified function
% ● Format
% [information, arguments] = pspm_help(func_name)
% ● Arguments
% func_name: the name of the function for help information
% ● Outputs
% information: the description of the specific function
% arguments: the arguments of the specific function
% ● History
% Introduced in PsPM 6.0
% Written and maintained in 2022 by Teddy Chao (UCL)
global settings
if isempty(settings)
pspm_init;
end
fid = fopen([settings.path,func_name,'.m'],'r');
% read the file into a cell array, one cell per line
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
A{i} = tline;
end
fclose(fid);
A = A.';
% get rid of empty lines
mk = cellfun(@ischar,A) & ~cellfun(@isempty,A);
A = A(mk);
% find matching lines
B = regexp(A,'^\s*%.*','match');
B = vertcat(B{:});
information = sort_info (B);
arguments = sort_args (B);
return
function A = sort_args (B)
% remove '% '
for i_line = 1:length(B)
C = B{i_line, 1};
if contains(C,'%')
C(strfind(C,'%'))=[];
end
B{i_line, 1} = C;
end
% sort
N_target = find(strcmp(B, ' ● Arguments'),1);
str = '';
while ( ~strcmp(B{N_target+1,1}(1),'●') )
str = [str, B{N_target+1, 1}];
N_target = N_target + 1;
if strcmp(B{N_target+1,1}(1:2),' ●')
break
elseif strcmp(B{N_target+1,1}(1),'%')
break
elseif strcmp(B{N_target+1,1}(1:2),' 1')
break
end
end
D = remove_multiple_space(str);
idx_var = strfind(D,': ');
idx_fst = strfind(D,'. ');
A = cell(length(idx_var), 2);
for i_var = 1:length(idx_var)
switch i_var
case 1
A{1,1} = D(1:(idx_var(1)-1));
A{1,2} = D((idx_var(1)+2):idx_fst(1));
% A.(var_name) = var_disc;
case length(idx_var)
A{length(idx_var),1} = D((idx_fst(end)+2):(idx_var(end)-1));
A{length(idx_var),2} = D((idx_var(end)+2):end);
% A.(var_name) = var_disc;
otherwise
A{i_var,1} = D((idx_fst(i_var-1)+2):(idx_var(i_var)-1));
A{i_var,2} = D((idx_var(i_var)+2):idx_fst(i_var));
end
end
function A = sort_info (B)
% remove '% '
for i_line = 1:length(B)
C = B{i_line, 1};
if C(1) == ' '
break
end
if contains(C,'% ')
C(strfind(C,'% '):(strfind(C,'% ')+1))=[];
end
B{i_line, 1} = C;
end
D = {'Description', 'Format'};
% sort
A = struct();
for i_D = 1:length(D)
N_target = find(strcmp(B, ['● ', D{i_D}]),1);
str = '';
while ( ~strcmp(B{N_target+1,1}(1),'●') )
str = [str, B{N_target+1, 1}];
N_target = N_target + 1;
if N_target == length(B)
break
elseif strcmp(B{N_target+1,1}(1),'●')
break
elseif strcmp(B{N_target+1,1}(1),'%')
break
end
end
A.(D{i_D}) = remove_multiple_space(str);
end
function B = remove_multiple_space (A)
% B is a string with multiple spaces
% A is the processed B where multiple spaces were converted into one space
B = A;
idx_space = strfind(A,' ');
idx_space_preserve = zeros(1,length(idx_space));
if idx_space(1) == 1
idx_space_preserve(1) = 1;
end
for i = 2:length(idx_space)
if idx_space(i) == idx_space(i-1)+1
idx_space_preserve(i) = 1;
end
end
idx_space_remove = nonzeros(idx_space.*idx_space_preserve);
B(idx_space_remove) = [];