9

I want to remove hyphens (-), slashes (/) and white space () from a string 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?

4 Answers 4

10

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'));
Sign up to request clarification or add additional context in comments.

2 Comments

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. :) Seriously -nice.
I was already using regexp and just wanted to know what are legal characters, so no new problems for me... ;-)
6

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', '[- \/]', '')

4 Comments

+1: genvarname is the correct solution to the overall problem. Regular expressions, however, are a heavyweight solution to a simple problem.
GENVARNAME is interesting, but it only removes the spaces. It replaces the other characters by their hex representations, which makes for a hideous name to use for a structure field.
As I said above, it will make the name look ugly, but it guarantees your name is valid and retains your original uniqueness. If you want to manually replace / delete chars, you have to be careful to 1) remove all invalid chars 2) not destroy your original uniqueness.
hang: The "not destroying uniqueness" point is a good one, although it would be kind of odd if the hyphens, slashes, and such were the only things making the strings unique.
0

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)) = [];

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.