This is a static copy of a profile report

Home

genvarname (1 call, 0.010 sec)
Generated 05-Nov-2014 07:52:38 using cpu time.
function in file /usr1/opt/matlab/7.13/toolbox/matlab/lang/genvarname.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
openfig>getTokensubfunction1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
85
illegalChars = unique(varname(...
10.010 s100.0%
142
varname = varnameCell{1};
10 s0%
141
if wasChar
10 s0%
138
end
10 s0%
127
if numPrecedingDups(i)>0 
10 s0%
All other lines  0 s0%
Totals  0.010 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
uniquefunction10.010 s100.0%
iskeywordfunction10 s0%
dec2hexfunction10 s0%
intmaxfunction10 s0%
genvarname>isCellStringsubfunction20 s0%
Self time (built-ins, overhead, etc.)  0 s0%
Totals  0.010 s100% 
Code Analyzer results
Line numberMessage
112{ A{:} B } can often be replaced by [ A {B}], which can be much faster.
112{ A{:} B } can often be replaced by [ A {B}], which can be much faster.
Coverage results
[ Show coverage for parent directory ]
Total lines in function145
Non-code lines (comments, blank lines)72
Code lines (lines that can run)73
Code lines that did run39
Code lines that did not run34
Coverage (did run/can run)53.42 %
Function listing
   time   calls  line
1 function varname = genvarname(candidate,protected)
2 %GENVARNAME Construct a valid MATLAB variable name from a given candidate.
3 % VARNAME = GENVARNAME(CANDIDATE) returns a valid variable name VARNAME
4 % constructed from the string CANDIDATE. CANDIDATE can be a string or a
5 % cell array of strings.
6 %
7 % A valid MATLAB variable name is a character string of letters, digits
8 % and underscores, such that the first character is a
9 % letter and the length of the string is <= NAMELENGTHMAX.
10 %
11 % If CANDIDATE is a cell array of strings the resulting cell array of
12 % strings in VARNAME are guaranteed to be unique from one another.
13 %
14 % VARNAME = GENVARNAME(CANDIDATE, PROTECTED) returns a valid variable
15 % name which is different from any of the list of PROTECTED names.
16 % PROTECTED may be a string or cell array of strings.
17 %
18 % Examples:
19 % genvarname({'file','file'}) % returns {'file','file1'}
20 % a.(genvarname(' field#')) = 1 % returns a.field0x23 = 1
21 %
22 % okName = true;
23 % genvarname('ok name',who) % returns a string 'okName1'
24 %
25 % See also ISVARNAME, ISKEYWORD, ISLETTER, NAMELENGTHMAX, WHO, REGEXP.
26
27
28 % Copyright 1984-2010 The MathWorks, Inc.
29 % $Revision: 1.1.8.9 $ $Date: 2010/11/17 11:29:11 $
30
31
32 % Argument check
1 33 error(nargchk(1, 2, nargin, 'struct'))
34
35 % Set up protected list if it exists
1 36 if nargin < 2
1 37 protected = {};
38 elseif ~iscell(protected)
39 protected = {protected};
40 end
41
42 % Set up inputs for loop
1 43 wasChar = true; % flag to make sure char array is set back to char
1 44 if ~iscell(candidate)
1 45 varnameCell = {candidate};
46 else
47 varnameCell = candidate;
48 wasChar = false;
49 end
50
51 % Check first input type
1 52 if ~isCellString(varnameCell)
53 error(message('MATLAB:genvarname:wrongVarnameType'));
54 end
55
56
57 % Check second input type
1 58 if ~isCellString(protected)
59 error(message('MATLAB:genvarname:wrongProtectedType'));
60 end
61
62
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % Loop over all the candidates in varnameCell to make them valid names
1 65 for k = 1:numel(varnameCell)
66
1 67 varname = varnameCell{k};
68
1 69 if isvarname(varname) % Short-circuit if varname already legal
70 varnameCell{k} = varname;
1 71 else
72 % Insert x if the first column is non-letter.
1 73 varname = regexprep(varname,'^\s*+([^A-Za-z])','x$1', 'once');
74
75 % Replace whitespace with camel casing.
1 76 [~, afterSpace] = regexp(varname,'\S\s+\S');
1 77 for j=afterSpace
78 varname(j) = upper(varname(j));
79 end
1 80 varname = regexprep(varname,'\s+','');
1 81 if (isempty(varname))
82 varname = 'x';
83 end
84 % Replace non-word character with its HEXADECIMAL equivalent
0.01 1 85 illegalChars = unique(varname(regexp(varname,'[^A-Za-z_0-9]')));
1 86 for illegalChar=illegalChars
1 87 if illegalChar <= intmax('uint8')
1 88 width = 2;
89 else
90 width = 4;
91 end
1 92 replace = ['0x' dec2hex(illegalChar,width)];
1 93 varname = strrep(varname, illegalChar, replace);
1 94 end
95
96 % Prepend keyword with 'x' and camel case.
1 97 if iskeyword(varname)
98 varname = ['x' upper(varname(1)) varname(2:end)];
99 end
100
101 % Truncate varname to NAMLENGTHMAX
1 102 varname = varname(1:min(length(varname),namelengthmax));
103
1 104 varnameCell{k} = varname;
1 105 end
1 106 end
107
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % The following section is to uniquify
1 110 numPrecedingDups = zeros(numel(varnameCell),1);
111 % Update the protected to include other candidates that might clash
1 112 protectedAll = {varnameCell{:},protected{:}};
1 113 for i = 1:numel(varnameCell)
1 114 varname = varnameCell{i};
115
116 % Calc number of dups within the candidates
1 117 numPrecedingDups(i) = ...
118 numel(find(strcmp(varname,varnameCell(1:i-1))));
119
120 % Check if candidate dups with the protected
1 121 if any(strcmp(varname,protected))
122 numPrecedingDups(i) = numPrecedingDups(i) + 1;
123 end
124
125 % See if unique candidate is indeed unique - if not up the
126 % numPrecedingDups
1 127 if numPrecedingDups(i)>0
128 uniqueName = appendNumToName(varname, numPrecedingDups(i));
129 while any(strcmp(uniqueName, protectedAll))
130 numPrecedingDups(i) = numPrecedingDups(i) + 1;
131 uniqueName = appendNumToName(varname, numPrecedingDups(i));
132 end
133
134 % Replace the candidate with the unique string.
135 varnameCell{i} = uniqueName;
136 protectedAll{i} = uniqueName;
137 end
1 138 end
139
140 % Make sure return argument is the right type
1 141 if wasChar
1 142 varname = varnameCell{1};
143 else
144 varname = varnameCell;
145 end

Other subfunctions in this file are not included in this listing.