# matlab8 - String Functions Getting Numeric Equivalents of...

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

String Functions

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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');
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);

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

View Full Document
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
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

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

View Full Document
In general : st(v), where v is a vector of locations that will be included. Examples :
This is the end of the preview. Sign up to access the rest of the document.

## matlab8 - String Functions Getting Numeric Equivalents of...

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

View Full Document
Ask a homework question - tutors are online