开发者

How can I format strings for use as structure field names in MATLAB?

开发者 https://www.devze.com 2023-01-25 04:19 出处:网络
I want to remove hyphens (-), slashes (/) and white space () from a strin开发者_如何学运维g name(i) so that I can use it as a structure field name.

I want to remove hyphens (-), slashes (/) and white space () from a strin开发者_如何学运维g name(i) so that I can use it as a structure field name.

This is the ugly way I am currently doing it using the function strrep:

cell2mat(strrep(strrep(strrep(name(i), '-',''),'/',''),' ', ''))

I have also tried other variations, such as:

strrep(name(i),{'-','/'},{'',''});
strrep(name(i),['-','/'],['','']);

What is a more efficient way of doing this?


Note: I'm guessing your variable name is a cell array of strings, in which case you will want to use {} (i.e. content indexing) instead of () (i.e. cell indexing) to get the strings from it...

As with many problems in MATLAB, there are a number of different ways you can solve this...


Option 1: You could use the function REGEXPREP. The following removes hyphens, forward slashes, and whitespace:

newName = regexprep(name{i},'[-/\s]','');

The benefit here is that the \s will match and replace all whitespace characters, which includes a normal space (ASCII code 32) as well as tabs, newlines, etc..

If you want to be safe and remove every character that is not valid in a MATLAB variable/field name, you can simplify the above to this:

newName = regexprep(name{i},'\W','');


Option 2: If you don't need to worry about removing anything other than the 3 characters you listed, you could use the function ISMEMBER like so:

newName = name{i};
newName(ismember(newName,'-/ ')) = [];


Option 3: If you want to just keep everything that is an alphanumeric character and dump the rest (hyphens, whitespace, underscores, etc.), you could use the function ISSTRPROP:

newName = name{i};
newName = newName(isstrprop(newName,'alphanum'));


The easiest way is to use the built in function genvarname. It will make the name look uglier, but its guaranteed to be a valid name AND it will retain you original uniqueness.

If you just want to remove specific characters, you can use regexprep:

regexprep('foo- /foo- /foo', '[- \/]', '')


Strings are just arrays, so you could do something like:

name(name == '-' | name == '/' | name = ' ') = [];

With respect to your overall goal, there are many more characters that aren't valid in a struct name. You're bet off defining a set of allowable characters, and eliminating everything that isn't in that set.

e.g.:

function i = isAllowed(str)

i = (str >= '0' & str <= '9') ...
  | (str >= 'a' & str <= 'z') ...
  | (str >= 'A' & str <= 'Z');


...

name(~isAllowed(name)) = [];


Here's another solution:

name = 'some/path/file-name ext';    %# sample string
blacklist = {'-' '/' ' '};           %# list of character not allowed

idx = cell2mat( cellfun(@(c)strfind(name,c), blacklist, 'UniformOutput',false) );
name(idx) = '_';                    %# you can remove/replace those locations

>> name
 name =
 some_path_file_name_ext
0

精彩评论

暂无评论...
验证码 换一张
取 消