Page 89 - Basics of MATLAB and Beyond
P. 89

>> strrep(str,’go’,’am’)
                               ans  =
                               Iamnow
                               The replacement text need not be the same length as the text it is
                               replacing:
                               >> strrep(str,’go’,’eat snails’)
                               ans  =
                               I eat snails now

                               And the text to be replaced can occur more than once:
                               >> strrep(str,’o’,’e’)
                               ans  =
                               Igenew
                               To delete characters from a string, replace them with an empty string
                               ’’ or []:

                               >> strrep(str,’o’,’’)
                               ans  =
                               Ignw


                               26.4   Converting Numbers to Strings

                               The functions num2str and int2str are useful for general purpose con-
                               version of numbers to strings. The latter is for integers:

                               >> for i = 1:3
                               disp([’Doing loop number ’ int2str(i) ’ of 3’])
                               end
                               Doing loop number 1 of 3
                               Doing loop number 2 of 3
                               Doing loop number 3of 3

                               And num2str is for everything else:
                               >> for i = 1:3
                               disp([’Case ’ int2str(i) ’, sin = ’ num2str(sqrt(i))])
                               end
                               Case 1, sin = 1
                               Case 2, sin = 1.4142
                               Case 3, sin = 1.7321
                               The inputs can be vectors or matrices:






                               c   2000 by CRC Press LLC
   84   85   86   87   88   89   90   91   92   93   94