-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunderscorify.m
More file actions
34 lines (29 loc) · 873 Bytes
/
underscorify.m
File metadata and controls
34 lines (29 loc) · 873 Bytes
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
function outstr = underscorify(str,leadingnum)
% UNDERSCORIFY cleans up a string for use as a variable name
%
% UNDERSCORIFY(STR) replaces all non-alphanumeric characters in a string with
% underscores.
%
% UNDERSCORIFY(STR,LEADINGNUM) If LEADINGNUM is true (default) and the string
% starts with a number, the letter 'x' is appended to the beginning of
% the string.
%
% Created 20120712 JW
% Updated 20120814 to condense multiple _'s to a single _.
% Updated 20120912 to trim leading whitespace
if ~exist('leadingnum')
leadingnum=true;
end
outstr = strtrim(str);
outstr(~isstrprop(outstr,'alphanum')) = '_';
[idx1 idx2] = regexp(outstr, '_+');
toremove = [];
for c=1:length(idx1)
toremove = [toremove idx1(c)+1:idx2(c)];
end
outstr(toremove)=[];
if leadingnum
if isstrprop(outstr(1),'digit')
outstr = ['x' outstr];
end
end