MATLAB: Matlab does not recognise hyphen

hyphen not recognisedMATLAB

Hi all,
I'm trying to remove a simple hyphen "-" from a string array. But matlab does not seem to recognise the hyphen. I'm sourcing the text from a website and storing the text in a string array. Then by using strrep(myStringArray,'-','_') I'm trying to remove the hyphen. The weired thing is matlab does not remove it but when I stop the program in the debugger and locally execute this command again it works. Any thoughts on this highly appretiated!

Best Answer

Hi all,
thanks a lot for all your answers!!!
This is a really weired one I'm pulling the text from websites. I tried native2unicode() and renaming it (e.g. myNewArray). Nothing worked.
I finally worked around it by using the function "isletter" and using a for loop since isletter doesn't take a string array:
for iLine=1:length(myStringArray)
currentChangeLine=char(myStringArray(iLine));
idxUnderscore=strfind(currentChangeLine,'_'); % I have some underscores which I want to keep
idxWhiteSpace=find(isspace(currentChangeLine));
idxIsDigit=find(isstrprop(currentChangeLine,'digit')); % also I want to keep digits in the text
idxNotALetter=find(~isletter(currentChangeLine));
idxChange=setdiff(idxNotALetter,[idxUnderscore,idxWhiteSpace,idxIsDigit]);
% the line above is to work out the indices where it's not a letter and not a digit,white space or underscore
% i.e. this will filter out hyphens but alos other symbols like &,@, etc.
currentChangeLine(idxChange)='_'; % replace the hyphen with an underscore
myStringArray(iLine)=currentChangeLine;
end
It's not a neat solution but it worked!
Once again thanks a lot for all your help!
Related Question