[str] = make_valid_struct_key(str) given str, will replace all non alpha-numeric characters with underscores and will append 's' to the beginning of the string this will construct valid struct field names from any string NOTE: if you have two strings that are only differentiated by their non alpha-numeric characters, this will not maintain their unique characters
0001 function [str] = make_valid_struct_key(str) 0002 0003 % [str] = make_valid_struct_key(str) 0004 % 0005 % given str, will replace all non alpha-numeric characters with underscores 0006 % and will append 's' to the beginning of the string 0007 % 0008 % this will construct valid struct field names from any string 0009 % 0010 % NOTE: if you have two strings that are only differentiated by their 0011 % non alpha-numeric characters, this will not maintain their unique 0012 % characters 0013 % 0014 0015 % fb 2007/10/29 0016 0017 % if (~isstr(str)) 0018 % error(sprintf('the variable that you passed is not a string')); 0019 % end 0020 0021 str = regexprep(str,'\"',''); 0022 str = regexprep(str,'[^a-zA-Z0-9]','_'); 0023 str = strcat('s',str);