matlab8 - String Functions Getting Numeric Equivalents of...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
String Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Getting Numeric Equivalents of Chars >> double('abc xyz') % or 'abc xyz‘ + 0 ans = 97 98 99 32 120 121 122 >> double('ABC XYZ') ans = 65 66 67 32 88 89 90
Background image of page 2
Converting Numbers Back to Strings >> x = double('here is a text') x = 104 101 114 101 32 105 115 32 97 32 116 101 120 116 >> xst = char(x) xst = here is a text
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Converting to Upper Case Given a string st, convert all the chars to upper case. Observation : the difference between the lower case and upper case of the same character is same for all characters. >> double('a') - double('A') % or ‘a’-’A’ ans = 32 >> double('f') - double('F') ans = 32
Background image of page 4
Algorithm Given st : stvector = numeric vector of st For all elements in stvector do if current char lower case change the element to upper case
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
isLower Function function result = isLower(ch) if (double(ch) >= double(‘a’) && double(ch) <= double(‘z’)) result = true; else result = false; end % or result = (ch >= 'a' && ch <= 'z');
Background image of page 6
toUpper Function function upperst = toUpper(st) diff = double('a') - double('A'); stvector = double(st); for i=1 : length(st) if isLower(st(i)) stvector(i) = stvector(i) - diff; end end upperst = char(stvector);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alternative Solution function stUpper = toUpper(st) diff = 'a' - 'A'; stUpper = st; for i = 1: length(st) if (isLowerCase(st(i))) stUpper(i) = stUpper(i) - diff; end end
Background image of page 8
Converting string into integer function result = str2int( st ) result = 0; for i = 1:length(st) digitst = st(i); digit = double(digitst) - double('0'); % digitst - '0' also works result = 10 * result + digit; end
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In general : st(v), where v is a vector of locations that will be included. Examples :
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

matlab8 - String Functions Getting Numeric Equivalents of...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online