Is it possible to use logical indexing to specify between a number interval and include a value outside that interval.

1 view (last 30 days)
I'm trying to use logical vectoring to exclude certain characters by using a mask on a string. So say I had a string like str='This !is @tes#t'. I'm trying to get "This is test". I'm donig this by converting the strings to doubles and then setting all characters to upper. Finally creating a mask that is between 65 and 90. I also need the mask to include 32 so I can keep spaces between words. How can I include that? Alternatively is there a better way to only include the alphabetic characters in a mask?
str='This !is @tes#t'
Ustr=upper(str)
Mask = 65<=Ustr & Ustr<=90

Accepted Answer

James Tursa
James Tursa on 31 Jan 2019
Edited: James Tursa on 31 Jan 2019
E.g.,
Mask = ismember(str,[' ','a':'z','A':'Z']);
Or using your method
Mask = (65<=Ustr & Ustr<=90) | Ustr == 32;

More Answers (1)

Steven Lord
Steven Lord on 31 Jan 2019
You could use the isstrprop function to keep alphabetic letters and whitespace characters.
str='This !is @tes#t';
letters = isstrprop(str, 'alpha');
spaces = isstrprop(str, 'wspace');
str(letters | spaces)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!