Replace bad character from a string Matlab

matlab remove special characters from string
matlab remove characters from string
matlab replace character in string
replace portion of string matlab
replace part of a string in matlab
matlab remove characters from cell array
matlab replace cell string
matlab string replace wildcard

I try to create a function that take a char in paramater :

function replaceBadCharacters(name)

    BadChar = '<>:"/\|?*';
    bad = ismember(BadChar, name);
    if contains(name, ' ') || any(BadChar)
        name = strrep(name, ' ', '_'); 
        name = strrep(name, BadChar(bad), '_'); 
    end
end

and i would like to replace space and BadChar from the char and replace it by a _. But my function isn't working.

For example i have a char Manager principal or Manager/principal and i would like to change it to Manager_principal

Thanks for helping.

Your function works correctly but hasn't any defined return parameters. Replace the first line by

function name_corrected = replaceBadCharacters(name)

and it'll work. Compare the documentation or type doc function in the MATLAB command line to study the full syntactic possibilities.

How to remove spaces and special characters from elements of a , Learn more about arrays, cell arrays MATLAB. I'm sure you've tried strrep(), but what was wrong with that? And do you want to replace all "special" characters with underlines, like But it is a bit confusing what you are asking, especially since you are using a version before the string() data type. newStr = strrep (str,old,new) replaces all occurrences of old in str with new. If any input argument is a nonscalar string array or cell array of character vectors, then the other input arguments must have compatible sizes.

EDIT: As stated in gehbiszumeis' answer, the actual problem is the missing return value. I'm sorry for not pointing that out in the first place.


Your function can be simplified to this:

function name = replaceBadCharacters(name)

  BadChar = '<>:"/\|?* ';
  bad = ismember(name, BadChar);
  name(bad) = '_';

end

You can simply add the "space" to your BadChar char array. Also, please have a look at Indexing with Logical Values.

Some tests:

>> replaceBadCharacters('Manager principal')
ans = Manager_principal

>> replaceBadCharacters('Manager/principal')
ans = Manager_principal

>> replaceBadCharacters('Manager?principal')
ans = Manager_principal

Replace any string in matrix with NaN - MATLAB Answers, My measurement file usually inserts bad when a measurement couldn't be made however some floating point have been corrupted and Want to replace these� newStr = erase (str,match) deletes all occurrences of match in str. The erase function returns the remaining text as newStr. If match is a string array or a cell array of character vectors, then erase deletes every occurrence of every element of match in str. The str and match arguments do not need to be the same size.

I think using replace is the best option and if you can use string you get a performance boost too. Here is a comparison:

>> replaceBadCharacters_ismember("Manager principal");
Elapsed time is 9.271434 seconds.

>> replaceBadCharacters_replace_char("Manager principal");
Elapsed time is 2.021813 seconds.

>> replaceBadCharacters_replace_string("Manager principal");
Elapsed time is 0.856642 seconds.

function result = replaceBadCharacters_ismember(name)

    BadChar = '<>:"/\|?* ';

    tic
    for i = 1:1e6
        result = name;
        bad = ismember(result, BadChar);
        result(bad) = '_';
    end
    toc
end

function result = replaceBadCharacters_replace_char(name)

    name = char(name);
    BadChar = ["<" ">" """" "\" "/" "|" "?" "*" " "];

    tic
    for i = 1:1e6
        result = replace(name, BadChar, '_');
    end
    toc
end

function result = replaceBadCharacters_replace_string(name)

    name = string(name);
    BadChar = ["<" ">" """" "\" "/" "|" "?" "*" " "];

    tic
    for i = 1:1e6
        result = replace(name, BadChar, '_');
    end
    toc
end

Searching and replacing in strings works oddly, Searching and replacing in strings works oddly. Learn more about string, replace, special characters MATLAB. Im having difficulty in deleting pre-specified characters from any given string. The characters that i am tryin to eliminate are 't' 'i' 'x' 'y' I need a lot of help creating a function that would do this for any given string.

Search string for special characters - MATLAB Answers, How can I search the string for special characters? I want to regexp('bad()nam! e.txt',rgx). ans = [] You can replace the special characters with something which works. https://uk.mathworks.com/help/matlab/ref/strrep.html� Replacing characters in a string. Learn more about regexp, regexprep, character, replace

How to remove last characters from String using mod?, Where you failed was in the first step. You did this: rem = mod(text,4);. String that indicates the start of the substring to extract, specified as a string array, a character vector, or a cell array of character vectors. extractAfter excludes startStr from the substring to extract.

Replace String with a NaN in table - MATLAB Answers, Replace String with a NaN in table. Learn more about table, string, 'bad', nan MATLAB. MATLAB stores all characters as Unicode characters. Both strings and character vectors use the same encoding. You can convert characters to their Unicode code values, and numbers to characters. Regular Expressions. Regular expressions are a versatile way to search and replace text. Lookahead Assertions in Regular Expressions

Comments
  • The function does not work correctly, if contains(name, ' ') || any(BadChar) does not what OP think it does.
  • Thanks @gehbiszumeis ! But when i call the function in my main file like this : replaceBadCharacters(component_NameValue); (component_NameValue is a char value which contains "Manager principal") it never enter in the function i don't know why.
  • @gehbiszumeis when i try my function in a simple .m file it's working but when i put under my main function the function replaceBadCharacters() it isn't working
  • Originally, MATLAB wants you to put every function in a separate *.m, add its path to the workspace and call it in your script equally to any other (builtin) MATLAB function. As of MATLAB2016b you can also add local functions to your script (which I understand is what you want to do) by include the function AT THE END of a script file
  • BTW: @rinkert gave a nice hint about, what in your function is incorrect, while it still does what you want. Another hint on that: Is there a situation, where the statement any(BadChar) is not true?
  • Thanks @HansHirse ! I will probably use your function as it is shortest and clearest than mine
  • Nice answer! Instead of tic/toc, do timeit(@()replaceBadCharacters_ismember("Manager principal")). In this case, you can remove the loop from each of the functions you want to time. timeit takes care of running your function as often as necessary to obtain an accurate estimate of time taken. If the time is too short, use a longer string as input.