12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

26.2 Comparing StringsThe = = test is not a good idea with strings because it compares theascii values <strong>of</strong> the strings, which must have the same length; if thestrings are not the same length, you get an error. The strcmp comm<strong>and</strong>avoids this difficulty:>> c1 = ’blond’;>> c2 = ’brown’;>> c3= ’blonde’;>> c1 = = c2ans =1 0 1 0 0>> c2 = = c3??? Array dimensions must match for binary array op.>> strcmp(c2,c3)ans =026.3 String ManipulationsTyping help strfun displays the full set <strong>of</strong> comm<strong>and</strong>s for working withstrings. A common example is to identify words within a string bysearching for whitespace (blank characters, tabs, etc.):>> str = ’I go now’;>> isspace(str)ans =0 1 0 0 1 0 0 0You can also search for letters:>> isletter(str)ans =1 0 1 1 0 1 1 1To find where a shorter string occurs within a longer one, use thefindstr comm<strong>and</strong>:>> pos = findstr(str,’go’)pos =3>> pos = findstr(str,’o’)pos =4 7To replace one string with another, use the strrep comm<strong>and</strong>:c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!