Path: blob/master/Week 9/Programming Assignment - 8/ex8/lib/makeValidFieldName.m
625 views
function str = makeValidFieldName(str)1% From MATLAB doc: field names must begin with a letter, which may be2% followed by any combination of letters, digits, and underscores.3% Invalid characters will be converted to underscores, and the prefix4% "x0x[Hex code]_" will be added if the first character is not a letter.5isoct=exist('OCTAVE_VERSION','builtin');6pos=regexp(str,'^[^A-Za-z]','once');7if(~isempty(pos))8if(~isoct)9str=regexprep(str,'^([^A-Za-z])','x0x${sprintf(''%X'',unicode2native($1))}_','once');10else11str=sprintf('x0x%X_%s',char(str(1)),str(2:end));12end13end14if(isempty(regexp(str,'[^0-9A-Za-z_]', 'once' ))) return; end15if(~isoct)16str=regexprep(str,'([^0-9A-Za-z_])','_0x${sprintf(''%X'',unicode2native($1))}_');17else18pos=regexp(str,'[^0-9A-Za-z_]');19if(isempty(pos)) return; end20str0=str;21pos0=[0 pos(:)' length(str)];22str='';23for i=1:length(pos)24str=[str str0(pos0(i)+1:pos(i)-1) sprintf('_0x%X_',str0(pos(i)))];25end26if(pos(end)~=length(str))27str=[str str0(pos0(end-1)+1:pos0(end))];28end29end303132