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.

strrep(str,’go’,’am’)ans =I am nowThe replacement text need not be the same length as the text it isreplacing:>> strrep(str,’go’,’eat snails’)ans =I eat snails nowAnd the text to be replaced can occur more than once:>> strrep(str,’o’,’e’)ans =I ge newTo delete characters from a string, replace them with an empty string’’ or []:>> strrep(str,’o’,’’)ans =I g nw26.4 Converting Numbers to StringsThe functions num2str <strong>and</strong> int2str are useful for general purpose conversion<strong>of</strong> numbers to strings. The latter is for integers:>> for i = 1:3disp([’Doing loop number ’ int2str(i) ’ <strong>of</strong> 3’])endDoing loop number 1 <strong>of</strong> 3Doing loop number 2 <strong>of</strong> 3Doing loop number 3<strong>of</strong> 3And num2str is for everything else:>> for i = 1:3disp([’Case ’ int2str(i) ’, sin = ’ num2str(sqrt(i))])endCase 1, sin = 1Case 2, sin = 1.4142Case 3, sin = 1.7321The inputs can be vectors or matrices:c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!